convert gmt to eastern time in python

In Python, you can convert GMT time to Eastern time using the pytz library. Here's an example:

main.py
import pytz
from datetime import datetime

# create a datetime object for a specific GMT time
gmt_time = datetime(2021, 1, 1, 12, 0, 0, tzinfo=pytz.utc)

# convert the datetime object to Eastern time
eastern_time = gmt_time.astimezone(pytz.timezone('US/Eastern'))

# print the result
print(eastern_time)
304 chars
12 lines

This will output: 2021-01-01 07:00:00-05:00

Note that the -05:00 at the end of the output indicates the timezone offset from UTC for Eastern time. If you want to display the Eastern time in a specific format, you can use the strftime() method of the datetime object, like this:

main.py
formatted_time = eastern_time.strftime('%Y-%m-%d %H:%M:%S %Z')
print(formatted_time)
85 chars
3 lines

This will output: 2021-01-01 07:00:00 EST

gistlibby LogSnag