Working with Dates and Times in Python
Max Shron
Data Scientist and Author
# Import relevant classes
from datetime import datetime, timedelta, timezone
# Import relevant classes from datetime import datetime, timedelta, timezone # US Eastern Standard time zone ET = timezone(timedelta(hours=-5))
# Timezone-aware datetime dt = datetime(2017, 12, 30, 15, 9, 3, tzinfo = ET)
print(dt)
2017-12-30 15:09:03-05:00
# India Standard time zone IST = timezone(timedelta(hours=5, minutes=30))
# Convert to IST print(dt.astimezone(IST))
2017-12-31 01:39:03+05:30
print(dt)
2017-12-30 15:09:03-05:00
print(dt.replace(tzinfo=timezone.utc))
2017-12-30 15:09:03+00:00
# Change original to match UTC
print(dt.astimezone(timezone.utc))
2017-12-30 20:09:03+00:00
Working with Dates and Times in Python