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

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.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

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:

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top