Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

ISO Datetimes With Timezones In Python

python
import pytz

from dateutil.parser import isoparse

def make_eastern_time(*, input):
    naive_datetime = isoparse(input)
    nyc_timezone = pytz.timezone("America/New_York")
    nyc_aware_datetime = nyc_timezone.localize(naive_datetime)
    iso_formatted_string = nyc_aware_datetime.isoformat('T', 'seconds')
    return iso_formatted_string
    
# Example: Eastern Daylight Time
print(make_eastern_time(input='2020-07-15T12:30:00'))
# >>>>>>>>>>>>>>>>>>>>> OUTPUT 2020-07-15T12:30:00-04:00

# Example: Eastern Standard Time
print(make_eastern_time(input='2020-11-15T12:30:00'))
# >>>>>>>>>>>>>>>>>>>>> OUTPUT 2020-11-15T12:30:00-05:00