我怎样才能比较Django中的两个日期时间

时间:2017-06-03 20:22:35

标签: django

我无法比较两个日期时间变量。其中一个是我的models.py中的DateTimeField

SomeInfo.objects.annotate(
    last_pubdate=Max('people__someinfo__data_pubdate')
).filter(last_pubdate=F('data_pubdate'))

其次是datetime.now。这是比较代码的示例

date_of_end_discount = models.DateTimeField()

但我有TypeError:

from datetime import datetime

def is_discount(self):
    return self.date_of_end_discount > datetime.now()

1 个答案:

答案 0 :(得分:0)

AFAIK datetime.now()不支持时区。

Django为此提供了一个帮手,需要pytz

words.count++;
catList[0].count++;

或者您可以使用timezone而不是datetime来解决此问题。

 naive = datetime.replace(tzinfo=None)

您现在应该可以比较日期了。

相关问题