Home > Net >  How to get EST timezone in Python
How to get EST timezone in Python

Time:03-30

I would like to get the EST time with Python. I have the following code:

import datetime
from pytz import timezone
import time
now_EST = datetime.datetime.today().astimezone(timezone('EST'))
print(now_EST)

And the output is:

2022-03-29 09:52:55.130992-05:00

But when I google the EST time zone, I find out that the time right now is 10:52 am EST, which essentially is the right time.

Why does my code show the 1 hour earlier time compared to the correct one?

CodePudding user response:

Daylight Saving Time. Try "EST5EDT"

CodePudding user response:

use a proper IANA time zone name to avoid ambiguities of the abbreviations.

from datetime import datetime
import pytz

print(datetime.now(pytz.timezone("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00

If you happen to use Python 3.9 or higher, use the built-in zoneinfo module to set the time zone (pytz is deprecated):

from datetime import datetime
from zoneinfo import ZoneInfo

print(datetime.now(ZoneInfo("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00
  • Related