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.
-
Read more: Number game questions
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−
Output
Read more: Design your own oval office online
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.
-
Read more: Number game questions
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.
-
Read more: Snahp it
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−
Output
Read more: Design your own oval office online
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.
- 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?