Python convert time string timezone

joel
1 min readSep 22, 2020

--

localFormat = “%Y-%m-%dT%H:%M:%S.000000000Z”

# first specify string format
localFormat = “%Y-%m-%dT%H:%M:%S.000000000Z”
# construct a datetime from string
localmoment_naive = datetime.strptime('2020-09-22T01:02:03.000000000Z', localFormat)
# set timezone to utc
utcomment = localmoment_naive.replace(tzinfo=pytz.utc)
# convert time to expected timezone
localDatetime = utcomment.astimezone(pytz.timezone("America/Toronto"))
# show it
localDatetime

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response