Categories
Python Answers

How to display the time in a different time zone with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *