Python Datetime : use strftime() with a timezone-aware date
Python Datetime : use strftime() with a timezone-aware date
In addition to what @Slam has already answered:
If you want to output the UTC time without any offset, you can do
from datetime import timezone, datetime, timedelta
d = datetime(2009, 4, 19, 21, 12, tzinfo=timezone(timedelta(hours=-2)))
d.astimezone(timezone.utc).strftime(%Y-%m-%d %H:%M:%S.%f)
See datetime.astimezone in the Python docs.
The reason is python actually formatting your datetime object, not some UTC at this point of time
To show timezone in formatting, use %z
or %Z
.
Look for strf docs for details
Python Datetime : use strftime() with a timezone-aware date
This will convert your local time to UTC and print it:
import datetime, pytz
from dateutil.tz.tz import tzoffset
loc = datetime.datetime(2009, 4, 19, 21, 12, tzinfo=tzoffset(None, -7200))
print(loc.astimezone(pytz.utc).strftime(%Y-%m-%d %H:%M:%S.%f) )