Django object - 'datetime.date' does not have the attribute 'tzinfo'

Here is my code that I use to report datetime timezone. I tried using the recommended approach from Django docs.

tradeDay = day.trade_date + timedelta(hours=6) td1 = pytz.timezone("Europe/London").localize(tradeDay, is_dst=None) tradeDay = td1.astimezone(pytz.utc) 

I get tz_info error. How can I datetime attribute tz_info?

USE_TZ = True in settings.py

+7
source share
1 answer

It seems that day.trade_date is actually a datetime.date object and not datetime.datetime so trying to localize it will result in an error.

Try day.trade_date convert day.trade_date to datetime.datetime with combine() . Then you can add 6 hours and localize it.

 # Convert to a datetime first tradeDate = datetime.combine(day.trade_date, datetime.min.time()) # Now the date can be localized tradeDay = tradeDate + timedelta(hours=6) td1 = pytz.timezone("Europe/London").localize(tradeDay, is_dst=None) tradeDay = td1.astimezone(pytz.utc) 
+10
source

Source: https://habr.com/ru/post/988685/


All Articles