Python pytz – Get current time in different time zone

In this post we will be creating a Python script that will get the current time in a different time zone. To do this we will be using the ‘pytz’ library which allows accurate and cross platform time zone calculations.

See the snippet of code below where we utilize the pytz library to retrieve the current time in the Japan time zone. To do this we create a datatime object and then covert this to a different time zone.

import datetime
import pytz

japan ='Japan'))
print("Time in Japan: ", japan.strftime('%Y-%m-%d %H:%M:%S'))

See another example below where we retrieve the current time in Mexico.

import datetime
import pytz

Mexico ='Mexico/General'))
print("Time in Mexico: ", Mexico.strftime('%Y-%m-%d %H:%M:%S'))

Using this Python code you may retrieve the time in a different time zone, including those not covered in the above examples. If you are interested in using some of the other time zones the pytz library has to offer, then click here.

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply