Can't subtract datetime and timestamp in django?

后端 未结 2 992
[愿得一人]
[愿得一人] 2021-02-01 10:20

I have a field timestamp = models.DateTimeField(auto_now_add=True) in the db. I want to find the difference between that timestamp and datetime.now().

When I tried datet

2条回答
  •  终归单人心
    2021-02-01 10:52

    Holá
    The short answer is:

    tz_info = your_timezone_aware_variable.tzinfo
    
    diff = datetime.datetime.now(tz_info) - your_timezone_aware_variable:
    

    You must add the timezone info to your now() time.
    But you must add the same timezone of the variable, thats why I first read the tzinfo attribute.

提交回复
热议问题