How to convert UTC to EST with Python and take care of daylight saving automatically?

You’ll need to use the pytz module (available from PyPI):

import pytz
from datetime import datetime

est = pytz.timezone('US/Eastern')
utc = pytz.utc
fmt="%Y-%m-%d %H:%M:%S %Z%z"

winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)
summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc)

print(winter.strftime(fmt))
print(summer.strftime(fmt))

print(winter.astimezone(est).strftime(fmt))
print(summer.astimezone(est).strftime(fmt))

which will print:

2016-01-24 18:00:00 UTC+0000
2016-07-24 18:00:00 UTC+0000
2016-01-24 13:00:00 EST-0500
2016-07-24 14:00:00 EDT-0400

The reason why you’ll need to use 'US/Eastern' and not 'EST' is exemplified in the last two lines of output.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)