Compare two date-times

Oct 09, 2013 · by Tim Kamanin

To compare two times you need to do the following:

from datetime import datetime,timedelta
d =

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:


Now you can do a comparison.

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials