Can't compare naive and aware <= challenge.datetime_end


I am trying to compare the current date and time with dates and times specified in models using comparison operators:

if challenge.datetime_start <= <= challenge.datetime_end:

The script errors out with:

TypeError: can't compare offset-naive and offset-aware datetimes

The models look like this:

class Fundraising_Challenge(models.Model):
    name = models.CharField(max_length=100)
    datetime_start = models.DateTimeField()
    datetime_end = models.DateTimeField()

I also have django using locale date and times.

What I haven't been able to find is the format django uses for DateTimeField(). Is it naive or aware? And how do I get to recognize locale datetime?

6/5/2019 10:48:12 PM

Accepted Answer

By default, the datetime object is naive in Python, so you need to make both of them either naive or aware datetime objects. This can be done using:

import datetime
import pytz


challenge.datetime_start = utc.localize(challenge.datetime_start) 
challenge.datetime_end = utc.localize(challenge.datetime_end) 
# now both the datetime objects are aware, and you can compare them

Note: This would raise a ValueError if tzinfo is already set. If you are not sure about that, just use

start_time = challenge.datetime_start.replace(tzinfo=utc)
end_time = challenge.datetime_end.replace(tzinfo=utc)

BTW, you could format a UNIX timestamp in datetime.datetime object with timezone info as following

d = datetime.datetime.utcfromtimestamp(int(unix_timestamp))
d_with_tz = datetime.datetime(
1/9/2018 9:11:22 PM is not timezone aware.

Django comes with a helper for this, which requires pytz

from django.utils import timezone
now =

You should be able to compare now to challenge.datetime_start

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow