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,
10/21/2021 08:18:19
Now you want to convert it to UTC timezone, and the result should be like,
10/21/2021 02:48:19
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.
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)
Output:
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)
Output:
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)
Output:
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
Summary:
We learned how to convert local time to UTC timezone in python.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.