Categories
Python Answers

How to fix TypeError: can’t compare offset-naive and offset-aware datetimes with Python?

Spread the love

To fix TypeError: can’t compare offset-naive and offset-aware datetimes with Python, we can use the utc.localize method to convert both times to aware datetimes.

For instance, we write

import datetime
import pytz

utc=pytz.UTC

challenge.datetime_start = utc.localize(challenge.datetime_start) 
challenge.datetime_end = utc.localize(challenge.datetime_end) 

to call utc.localize to convert datetime_start and datetime_end to time zone aware datetimes.

Then we can compare their values directly.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.