Sometimes, we want to display the time in a different time zone with Python.
In this article, we’ll look at how to display the time in a different time zone with Python.
How to display the time in a different time zone with Python?
To display the time in a different time zone with Python, we can use the ZoneInfo
class.
For instance, w write
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
now_pacific = datetime.now(ZoneInfo('US/Pacific'))
print(now_pacific.isoformat(timespec='seconds'))
to create the datetime with the current date and time at the US/Pacific time zone with
now_pacific = datetime.now(ZoneInfo('US/Pacific'))
Then we use
now_pacific.isoformat(timespec='seconds')
to return the date time string in ISO 8601 (YYYY-MM-DD) format.
Conclusion
To display the time in a different time zone with Python, we can use the ZoneInfo
class.