How to change timezone in datetime 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−

from datetime import datetime from pytz import timezone format = "%Y-%m-%d %H:%M:%S %Z%z" now_utc = datetime.now[timezone['UTC']] print['Current Time in UTC TimeZone:',now_utc.strftime[format]] now_asia = now_utc.astimezone[timezone['Asia/Kolkata']] 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−

from datetime import datetime import pytz format = "%Y-%m-%d %H:%M:%S %Z%z" original_tz = pytz.timezone['Asia/Kolkata'] converted_tz = pytz.timezone['US/Eastern'] datetime_object = datetime.now[original_tz] print["Original Date & Time: in Asia/Kolkata ",datetime_object.strftime[format]] datetime_object = datetime.now[converted_tz ] 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.

Updated on 22-Sep-2022 11:22:51

  • Related Questions & Answers
  • How to convert date and time into int in Android sqlite?
  • How to get current date and time in Python?
  • How to get formatted date and time in Python?
  • How to combine date and time from different MySQL columns to compare with the entire DateTime?
  • How to convert JS date time to MySQL datetime?
  • How to print current date and time using Python?
  • How to compare time in different time zones in Python?
  • How to access and convert time using the time library in Python
  • Python Basic date and time types
  • Convert VARCHAR Date to a different format in MySQL?
  • How to format date and time in android?
  • How to convert date to datetime in Python?
  • How can we do date and time math in Python?
  • How to convert ISO 8601 string to Date/time object in Android?
  • How to convert Python date in JSON format?

How do I change the timezone in datetime?

Use the datetime. astimezone[] method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.

How do I change timezone in Python?

To do this we will use one of the modules of python pytz . This module brings the Olson tz database into Python and this library allows accurate and cross-platform timezone calculations using Python. The method pytz. timezone[] generates the current timezone of a particular region.

How do I use datetime timezone in Python?

You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz. all_timezones print[zones] # Output: all timezones of the world.

How do I change timezone to UTC in Python?

How to convert local datetime to UTC in Python.
local_time = pytz. timezone["America/New_York"].
naive_datetime = datetime. datetime. ... .
local_datetime = local_time. localize[naive_datetime, is_dst=None] ... .
utc_datetime = local_datetime. astimezone[pytz. ... .
print[utc_datetime] Output..

Bài mới nhất

Chủ Đề