How to convert date and time with different timezones in Python?



In this article, we will show you how to convert date and time with different timezones in python.

  • Using astimezone() function

  • Using datetime.now() function

The easiest way in Python date and time to handle timezones is to use the pytz module. This library allows accurate and cross−platform timezone calculations. pytz brings the Olson tz database into Python. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (datetime.tzinfo)

Before you use it you'll need to install it using −

pip install pytz

Method 1: Using astimezone() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the datetime(To work with dates and times, Python has a module called datetime) module.

  • Use the import keyword, to import timezone from pytz module.

  • Enter the date format as a string using the input() function and create a variable to store it.

  • Get the current time using the datetime.now() function(returns the current local date and time) and pass the timezone() function(gets the time zone of a specific location) with the timezone as an argument to it say ‘UTC’.

  • Format the above DateTime using the strftime() and print it.

  • Conver the above time to another time zone using the datetime.astimezone() function(The datetime.astimezone() function is used to modify objects of the datetime class in the datetime module) by passing a new time zone as an argument to it.

  • Format the above converted DateTime using the strftime() and print it.

Example

The following program returns the current time after being converted to another timezone using the astimezone() function−

# importing datetime from datetime import datetime # importing timezone from pytz module from pytz import timezone # giving the format of datetime format = "%Y-%m-%d %H:%M:%S %Z%z" # getting the current time in UTC timezone now_utc = datetime.now(timezone('UTC')) # Format the above DateTime using the strftime() print('Current Time in UTC TimeZone:',now_utc.strftime(format)) # Converting to Asia/Kolkata time zone now_asia = now_utc.astimezone(timezone('Asia/Kolkata')) # Format the above datetime using the strftime() print('Current Time in Asia/Kolkata TimeZone:',now_asia.strftime(format))

Output

On executing, the above program will generate the following output −

Current Time in UTC TimeZone: 2022-09-07 04:23:21 UTC+0000
Current Time in Asia/Kolkata TimeZone: 2022-09-07 09:53:21 IST+0530

Method 2: Using datetime.now() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the datetime(To work with dates and times, Python has a module called datetime) module.

  • Use the import keyword, to import timezone from pytz module.

  • Give the date time format as a string and store it in a variable.

  • Get the standard Asia/Kolkata timezone using the timezone() function(gets the time zone of a specific location) of the pytz module by passing the Asia/Kolkata timezone as an argument to it and store the result in a variable.

  • Get the standard US/Eastern timezone using the timezone() function of the pytz module by passing the US/Eastern timezone as an argument to it and store the result in a variable.

  • Get the current time using the datetime.now() function(returns the current local date and time) and pass the above first timezone i.e asia/kolkata time zone object as an argument to it(Here it converts the current date and time to the asia/kolkata timezone).

  • Use the strftime() function(returns a string that represents a datetime object based on the format codes) to format the above datetime object and print it.

strftime(format)
  • Get the current time using the datetime.now() function and pass the above second−time zone i.e 'US/Eastern' time zone as an argument to it(Here it converts the current date and time to the 'US/Eastern' timezone).

  • Use the strftime() function to format the above datetime object and print it.

Example

The following program returns the current time after being converted to another timezone using the astimezone() function−

# importing datetime from datetime import datetime # importing pytz module import pytz # giving the format of datetime format = "%Y-%m-%d %H:%M:%S %Z%z" # getting the standard UTC time original_tz = pytz.timezone('Asia/Kolkata') # giving the timezone to which it is to be converted converted_tz = pytz.timezone('US/Eastern') # Getting the current time in the Asia/Kolkata Time Zone datetime_object = datetime.now(original_tz) # Format the above datetime using the strftime() print("Original Date & Time: in Asia/Kolkata ",datetime_object.strftime(format)) # Getting the current time in the US/Eastern Time Zone datetime_object = datetime.now(converted_tz ) # Format the above datetime using the strftime() print("Converted Date & Time: in US/Eastern TimeZone ",datetime_object.strftime(format))

Output

On executing, the above program will generate the following output −

Original Date & Time: in Asia/Kolkata 2022-09-07 10:00:49 IST+0530
Converted Date & Time: in US/Eastern TimeZone 2022-09-07 00:30:49 EDT-0400

Conclusion

We learned how to convert date and time with different timezones in Python in this article. The timedelta() function was used to obtain the timezone object. We learned how to use the datetime.now() function to get the current date and time. We also learned how to use the strftime() function to format the datetime object.


Advertisements