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