Skip to content

How to convert Z hour/minute to time zones #3562

Answered by dopplershift
DaveJ83 asked this question in Q&A
Discussion options

You must be logged in to vote

As of Python 3.9, there's a zoneinfo module for timezones:

from datetime import datetime, UTC
from zoneinfo import ZoneInfo

t_str = '1546'

# Parse, then replace the date as well as specify that the timezone is UTC
dt = datetime.strptime(t_str, '%H%M').replace(year=2024, month=7, day=11, tzinfo=UTC)

# Get the UTC time as EDT
dt.astimezone(ZoneInfo('America/New_York'))

gives:
datetime.datetime(2024, 7, 11, 11, 46, tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by dopplershift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants