pandaZW pandaZW - 1 year ago 82
Python Question

How to compare two datetime by day in Django?

from django.utils import timezone

good = Goods.objects.get(id=num)
now =


How to compare these whether they are by one day.

2016-12-01 10:21:32.746505+00:00
2016-12-01 10:24:08.906268+00:00

Answer Source

Use a timedelta:

d1 = datetime(year=2016, month=12, day=1)
d2 = datetime(year=2016, month=12, day=2)

abs(d2 - d1) < timedelta(days=1)

Or if you just want to compare the day, you can access it from the .day attribute: # 1 # 2
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download