I would like to know how to convert ISO date to UTC in python(2.6.6)
date_dob = getattr(model, value)
tz=pytz.UTC
date_dob =tz.localize(date_dob)
print(date_dob)
Here date_dob is mapped to DB property with value ISODate("1989-08-27T00:00:00.000Z").My requirement is to convert it into a UTC date time.
Output 1989-08-27 00:00:00+00:00 ,I believe its not a UTC value
Is this what you want?
import dateutil.parser
datestring = '1989-08-27T00:00:00.000Z'
yourdate = dateutil.parser.parse(datestring)
print(yourdate)
from datetime import datetime
from pytz import timezone
date_str = '1989-08-27T00:00:00.000Z'
print(date_str)
datetime_obj = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC'))
print(datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('US/Eastern'))
print(datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.