Convert datetime to Different Time Zone in Python (3 Examples)
This tutorial demonstrates how to convert the time zone of a datetime object to another time zone such as CET,CST,EAT, EST, and UTC in the Python programming language.
This article contains the sections below:
Let’s dive into the Python code!
Import datetime Module & Create Example Data
As a first step, we have to import the datetime module to Python:
from datetime import datetime
To specify the different time zones in Python, we also have to load the pytz module:
import pytz
Now, we can create a datetime object with the time zone UTC by applying the Python code below:
my_datetime = datetime(2023, 2, 13, 17, 10, 27, tzinfo = pytz.utc) print(my_datetime) # 2023-02-13 17:10:27+00:00
We can also format our datetime object to show the time zone using the strftime function:
my_datetime_utc = my_datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z') print(my_datetime_utc) # 2023-02-13 17:10:27 UTC+0000
As you can see, our example date and time are the 13th of February 2023 at 05:10:27pm.
Let’s convert this date and time to different time zones.
Example 1: Convert datetime to CET Time Zone
If we want to convert a datetime object to the CET time zone, we can use the astimezone function and the time zone ‘Europe/Berlin’ as shown below:
my_datetime_cet = my_datetime.astimezone(pytz.timezone('Europe/Berlin')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(my_datetime_cet) # 2023-02-13 18:10:27 CET+0100
Example 2: Convert datetime to CST Time Zone
Similar to the Python code of Example 1, we can convert our example datetime object to CST by specifying ‘US/Central’ within the timezone function:
my_datetime_cst = my_datetime.astimezone(pytz.timezone('US/Central')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(my_datetime_cst) # 2023-02-13 11:10:27 CST-0600
Example 3: Convert datetime to EST Time Zone
In case we want to convert our time zone to EST, we have to specify ‘US/Eastern’:
my_datetime_est = my_datetime.astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(my_datetime_est) # 2023-02-13 12:10:27 EST-0500
Video, Further Resources & Summary
If there are any questions left, please have a look at the following video which I have published on my YouTube channel. I show and explain the examples of this tutorial in a more detailed way:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Do you need more explanations on how to change the time zones of datetime objects in Python? Then you should have a look at the following video of the technologyCult YouTube channel.
In the video, the speaker explains how to convert time zones using the pytz package in Python.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Not enough? You could have a look at some other tutorials here on Statistics Globe:
- Convert Epoch Time to datetime Object & Vice Versa in Python
- Make Unaware datetime Time Zone Aware in Python
- Convert datetime to Unix Timestamp in Python
- Convert datetime Object to Local Time Zone
- Python Programming Tutorials
This post has shown how to transform datetime objects to other time zones in the Python programming language.
Note that we could use the code of this tutorial to convert datetime objects to many other time zones such as AEST, ACST, AFT, AKST, AST, CAT, EET, MSK, MST, PST, WAT, and WET as well. We simply would have to specify the corresponding region within the timezone function.
In case you have further questions, you may leave a comment below.
This tutorial was created in collaboration with Gottumukkala Sravan Kumar. Please have a look at Gottumukkala’s author page to get more information about his academic background and the other articles he has written for Statistics Globe.
2 Comments. Leave new
Hi, awesome tutorial!
How can I use this on a list?
I have a json file with startTime and endTime that I’d like to convert by one hour off (From Norway time to Sweden time)
I have imported the json file and managed to set up the pytz, but I dont know how to use thr for loop for a timevalue?
Hello Mathias,
You can use the following code segment:
sweden_date_list = []
for datetime in norway_date_list:
sweden_date_list.append(datetime.astimezone(pytz.timezone("Europe/Stockholm")))
This will yield a new list with the Sweden timezones in the order the dates were in the Norway timezone list.
Please let us know if you have any further questions.
Best,
Ömer