To convert Eastern Time to GMT in Python, you can use the datetime module and the timezone class. Follow the steps below:
Import the datetime module
Create a datetime object representing the Eastern Time with the timezone set to 'US/Eastern'
Convert the Eastern Time datetime object to UTC/GMT by calling the astimezone() method and passing 'UTC' as the timezone argument
Format the resulting datetime object as a string in the desired format using the strftime() method
Here's the code:
main.py
import datetime
from pytz import timezone
# create a datetime object representing Eastern timeeastern = datetime.datetime.now(timezone('US/Eastern'))
# convert Eastern time to UTCutc = eastern.astimezone(timezone('UTC'))
# format UTC datetime object as a stringutc_string = utc.strftime('%Y-%m-%d %H:%M:%S')
print(utc_string)
333 chars
14 lines
This will output the current date and time in GMT/UTC time zone.