Compare two date-times

09 Oct · by Tim Kamanin · 1 min read

To compare two times you need to do the following:

from datetime import datetime,timedelta
d = datetime.now()

d1 = d+timedelta(minutes=20)

if d < d1:
  print 'Hello'

When you compare django's DateTimeField value with timedelta you can get an error: "can't compare offset-naive and offset-aware datetimes", to solve this, you need to convert DateTimeField from offset-aware to offset-naive:

self.created.replace(tzinfo=None)

Now you can do a comparison.

Comments

Required for comment verification