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.
Frequently Asked:
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.