Python convert timezone to utc

Convert Local datetime to UTC timezone in Python

In this article, we will discuss how to convert local datetime to UTC timezone in python.

Suppose you are in a timezone that is not UTC. But you have a datetime in your local timezone, and you want to convert it to UTC timezone. For example, if you are in Asia/Calcutta timezone and timestamp in your local time zone is,

Now you want to convert it to UTC timezone, and the result should be like,

Before we start looking into the solutions, an important point to understand is that these solutions will apply to all time zones. It means, whatever your local timezone is, you can easily convert the datetime to UTC format using these solutions. Let’s start looking into them one by one.

Convert local datetime string to UTC in Python

We will use the datetime module for this. First, we will create a datetime object from a string. This datetime object will have no timezone associated with it, and it means it can be considered as of local timezone. Then we will change the timezone of the datetime object to UTC by calling the astimezone() function on the datetime object.

Frequently Asked:

The astimezone() function accepts a timezone instance tz as an argument. It returns a new DateTime instance according to the specified time zone parameter tz, i.e., it converts the time in calling datetime to the specified timezone and returns a new datetime object containing it.

Let’s use this to convert local time to UTC i.e.

from datetime import datetime import pytz dt_str = "10/21/2021 8:18:19" format = "%m/%d/%Y %H:%M:%S" # Create datetime object in local timezone local_dt = datetime.strptime(dt_str, format) print('Datetime in Local Time zone: ', local_dt) # Convert local datetime to UTC time-zone datetime dt_utc = local_dt.astimezone(pytz.UTC) print('Datetime in UTC Time zone: ', dt_utc) dt_utc_str = dt_utc.strftime(format) print('Datetime string in UTC Time zone: ', dt_utc_str)
Datetime in Local Time zone: 2021-10-21 08:18:19 Datetime in UTC Time zone: 2021-10-21 02:48:19+00:00 Datetime string in UTC Time zone: 10/21/2021 02:48:19

Convert timezone of datetime object from local to UTC in Python

Instead of string, if you already have the datetime object with local timezone (or null timezone), we can convert it directly to the datetime object with UTC timezone using astimezone(). For example,

from datetime import datetime import pytz # Create datetime object in local timezone local_dt = datetime(2021, 10, 4, 9, 10, 34, 300030) print('Datetime in Local Time zone: ', local_dt) # Convert local datetime to UTC time-zone datetime dt_utc = local_dt.astimezone(pytz.UTC) print('Datetime in UTC Time zone: ', dt_utc)
Datetime in Local Time zone: 2021-10-04 09:10:34.300030 Datetime in UTC Time zone: 2021-10-04 03:40:34.300030+00:00

Convert the current local time to UTC in Python

Suppose we have a datetime object that contains the current time in the local timezone, and it has the timezone information associated with it. Using astimezone(), we can convert it to UTC timezone and get the current UTC. For that, we will pass the pytz.UTC as argument to astimezone() function. For example,

from datetime import datetime import pytz # Get current time in local timezone local_dt = datetime.now() print('Current Local Time: ', local_dt) # Convert local to UTC timezone dt_utc = local_dt.astimezone(pytz.UTC) print('Current time in UTC Time-zone: ', dt_utc)
Current Local Time: 2021-10-17 10:12:55.502825 Current time in UTC Time-zone: 2021-10-17 04:42:55.502825+00:00

We learned how to convert local time to UTC timezone in python.

Читайте также:  Python locals and vars

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

Читайте также:  Заголовок страницы

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

Convert datetime to Different Time Zone in Python (3 Examples)

TikTok Icon Statistics Globe

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:

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:

Читайте также:  Using svg icons css

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

If you accept this notice, your choice will be saved and the page will refresh.

accept

Accept YouTube Content

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

If you accept this notice, your choice will be saved and the page will refresh.

accept

Accept YouTube Content

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.

Источник

Оцените статью