I'm currently using Python 3.9.6 and have this simple code
import datetime
import pytz
est = pytz.timezone('America/New_York')
myDateTime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=00, tzinfo=est)
print("America/New_York: ", myDateTime.isoformat())
print("UTC: ", myDateTime.astimezone(pytz.utc).isoformat())
which give the result
America/New_York: 2024-08-15T17:00:00-04:56
UTC: 2024-08-15T21:56:00+00:00
Currently in New York it's Daylight Savings time. So I expected America/New_York to show time offset of -04:00
, but it's showing -4:56
. I'm wondering if anyone has an explanation for this.
Try like this:
import datetime
import pytz
est = pytz.timezone('America/New_York')
naive_datetime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=0)
# Localize it to the Eastern timezone
myDateTime = est.localize(naive_datetime)
print("America/New_York: ", myDateTime.isoformat())
print("UTC: ", myDateTime.astimezone(pytz.utc).isoformat())
When you create a datetime object with the tzinfo parameter like this:
myDateTime = datetime.datetime(year=2024, month=8, day=15, hour=17, minute=0, tzinfo=est)