Converting Datetime From One Time Zone To Another Using Pytz
I have a data set which includes date/timestamps from New York WITHOUT timezone information. EDT or EST are not recorded. Dates include daily data for several years, so it includes
Solution 1:
You can convert from New York time to Frankfurt time by using localize()
to create your naive datetime objects in New York time and astimezone()
to then convert them to Frankfurt (Berlin) time. Using the timezone name (rather than a specific timezone abbreviation that only applies during part of the year) will handle the daylight savings difference for you.
For example:
from datetime import datetime
from pytz import timezone
newyork_tz = timezone('America/New_York')
berlin_tz = timezone('Europe/Berlin')
newyork = newyork_tz.localize(datetime(2018, 5, 1, 8, 0, 0))
berlin = newyork.astimezone(berlin_tz)
print(newyork)
print(berlin)
# OUTPUT# 2018-05-01 08:00:00-04:00# 2018-05-01 14:00:00+02:00
Post a Comment for "Converting Datetime From One Time Zone To Another Using Pytz"