user8852465
user8852465

Reputation:

How to add tzinfo to date?

I have the following vars:

time_created = datetime.utcnow() and time_created_day = datetime.utcnow().date().

I cannot save time_created_day to the db because of AttributeError: 'datetime.date' object has no attribute 'tzinfo'

How to fix this (add tzinfo)?

Upvotes: 0

Views: 4436

Answers (1)

vskr
vskr

Reputation: 11

Ok, here is one way that I did this.

If you need to add tzinfo, you may use the below.

from datetime import datetime
import pytz
  time_created_day = datetime.datetime.utcnow().date()
  time_created_day_with_tz_info = datetime(time_created_day.year, 
  time_created_day.month, time_created_day.day, tzinfo=pytz.timezone('UTC'))


 > print time_created_day_with_tz_info

 > datetime.datetime(2018, 5, 9, 0, 0, tzinfo=<UTC>)

Upvotes: 1

Related Questions