Python Forum
utc time to est time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: utc time to est time (/thread-30701.html)



utc time to est time - Nickd12 - Nov-02-2020

ive been trying to figure out how to convert this utc date time format "2020-11-02T08:30:00-05:00" to a est "%Y-%m-%d %H:%M:%S" type format.


RE: utc time to est time - bowlofred - Nov-02-2020

Do you want it to figure out that because the TZ offset is -05:00 that it should be EST? Unfortunately, that's only an assumption. Other timezones besides US/Eastern may currently have a 5 hour offset.

Or do you want to take the input time and display it in the current US/Eastern local time? If you have 3.9 and a machine with a timezone DB (that probably means installing the tzdata module if you're on windows), then the following would work.

>>> u = '2020-11-02T08:30:00-05:00'
>>> import datetime
>>> import zoneinfo
>>> datetime.datetime.fromisoformat(u).astimezone(zoneinfo.ZoneInfo("US/Eastern")).strftime("%Y-%m-%d %H:%M:%S")
'2020-11-02 08:30:00'