Set datetime to Different Time Zone in Python (3 Examples)
This post shows how to transform the time zone of a datetime object to another time zone (UTC, CET, CST, EAT, EST) in Python.
Loading datetime Module & Generating Example Data
We can load the datetime module as shown below.
from datetime import datetime |
from datetime import datetime
We also have to import the pytz module to deal with the different time zones:
import pytz |
import pytz
For the different examples, the following datetime object with time zone UTC will be created:
date_x = datetime(2025, 5, 25, 13, 50, 30, tzinfo = pytz.utc) print(date_x) # 2025-05-25 13:50:30+00:00 |
date_x = datetime(2025, 5, 25, 13, 50, 30, tzinfo = pytz.utc) print(date_x) # 2025-05-25 13:50:30+00:00
By using the strftime() function, we can format our datetime object to show the time zone.
date_x_utc = date_x.strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_utc) # 2025-05-25 13:50:30 UTC+0000 |
date_x_utc = date_x.strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_utc) # 2025-05-25 13:50:30 UTC+0000
Example 1: Set datetime to EST Time Zone
To transform datetime object to the time zone EST, we can apply the astimezone() function and the time zone ‘US/Eastern’ as shown next:
date_x_est = date_x.astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_est) # 2025-05-25 09:50:30 EDT-0400 |
date_x_est = date_x.astimezone(pytz.timezone('US/Eastern')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_est) # 2025-05-25 09:50:30 EDT-0400
Example 2: Convert datetime to CST Time Zone
Similar to the code in the first example, we can transform our datetime object to the CST time zone by specifying ‘US/Central’ within the timezone() function:
date_x_cst = date_x.astimezone(pytz.timezone('US/Central')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_cst) # 2025-05-25 08:50:30 CDT-0500 |
date_x_cst = date_x.astimezone(pytz.timezone('US/Central')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_cst) # 2025-05-25 08:50:30 CDT-0500
Example 3: Set datetime to CET Time Zone
And if we want to convert our time zone to CET, we have to specify ‘Europe/Berlin’:
date_x_cet = date_x.astimezone(pytz.timezone('Europe/Berlin')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_cet) # 2025-05-25 15:50:30 CEST+0200 |
date_x_cet = date_x.astimezone(pytz.timezone('Europe/Berlin')).strftime('%Y-%m-%d %H:%M:%S %Z%z') print(date_x_cet) # 2025-05-25 15:50:30 CEST+0200
Further Resources
Please find some related tutorials below.
- Set datetime to Unix Timestamp in Python (2 Examples)
- Set String to datetime Object in Python (3 Examples)
- Set Unix Timestamp to datetime in Python (Example)
- All Python Programming Tutorials
Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.