How to compare datetime in Django?

独自空忆成欢 提交于 2019-11-30 10:10:31

I am assuming that pub_date is a django.db.models.DateField, which means you can treat it as a datetime.date object.

If you convert them to the same type (either datetime.datetime or datetime.date) and subtract one from the other, you will get an instance of datetime.timedelta.

As you are using datetime.datetime.now(), if your pub_date is simply a date rather than a datetime, you may as well use ds = datetime.date.today() instead:

>>> ds = datetime.date.today()
>>> dd = datetime.date(2009, 12, 9)
>>> ds - dd
datetime.timedelta(2) # 2 days ago
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!