This article will discuss how to convert UTC time (in string or as a datetime object) to local time in python.
Suppose you are in a timezone that is not UTC. But you have a datetime in UTC timezone, and you want to convert it to your local timezone. For example, you have a UTC timestamp as a string,
10/21/2021 8:18:19
Now you want to convert it to your local timezone. If you are in Asia/Calcutta timezone, then the result should be like,
10/21/2021 13:48:19
All the solutions we will discuss for converting UTC to local time, will apply to all time zones. It means, whatever your local timezone is, you can easily convert the UTC datetime to your local time using these solutions. Let’s start looking into them one by one.
Convert UTC datetime string to local time
We can use the datetime module for this. The steps are as follows,
- Create a datetime object from the UTC time string.
- This datetime object will have no timezone associated with it. Therefore assign the UTC timezone to this datetime object using replace(tzinfo=pytz.UTC) function.
- Convert the timezone of the datetime object to local timezone by calling the astimezone() function on datetime object.
The astimezone() function accepts a timezone instance tz as 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 see a complete example to convert UTC datetime string to local time i.e.
from datetime import datetime, tzinfo from dateutil import tz import pytz dt_str = "10/21/2021 8:18:19" format = "%m/%d/%Y %H:%M:%S" # Create datetime object in local timezone dt_utc = datetime.strptime(dt_str, format) dt_utc = dt_utc.replace(tzinfo=pytz.UTC) print('Datetime in UTC Time zone: ', dt_utc) # Get local timezone local_zone = tz.tzlocal() # Convert timezone of datetime from UTC to local dt_local = dt_utc.astimezone(local_zone) print('Datetime in Local Time zone: ', dt_local) local_time_str = dt_local.strftime(format) print('Time as string in Local Time zone: ', local_time_str)
Output:
Datetime in UTC Time zone: 2021-10-21 08:18:19+00:00 Datetime in Local Time zone: 2021-10-21 13:48:19+05:30 Time as string in Local Time zone: 10/21/2021 13:48:19
Convert UTC datetime object from local datetime
Instead of string, if you already have the datetime object with UTC timezone, we can convert it directly to a datetime object with a local timezone using astimezone(). For example,
from datetime import datetime from dateutil import tz import pytz # Create Datetime object with UTC timezone dt_utc = datetime(2021, 10, 4, 9, 10, 34, 300030, tzinfo=pytz.UTC ) print('Datetime in UTC Time zone: ', dt_utc) # Get local timezone local_zone = tz.tzlocal() # Convert UTC to local time zone local_dt = dt_utc.astimezone(local_zone) print('Datetime in Local Time zone: ', local_dt)
Output:
Datetime in UTC Time zone: 2021-10-04 09:10:34.300030+00:00 Datetime in Local Time zone: 2021-10-04 14:40:34.300030+05:30
Convert the current UTC datetime to local datetime in python
Suppose we have a datetime object that contains the current time in the UTC timezone and has the timezone information. Using astimezone(), we can convert it to a local timezone and get the current local time. For that, we will pass the local timezone as an argument to astimezone() function. For example,
from datetime import datetime from dateutil import tz import pytz # Get current datetime in UTC timezone utc_now = datetime.now(tz=pytz.UTC) print('Current Datetime in UTC: ', utc_now) # Get local timezone local_zone = tz.tzlocal() # Convert timezone of datetime from UTC to local local_now = utc_now.astimezone(local_zone) print('Current Datetime in Local Time zone: ', local_now)
Output:
Current Datetime in UTC: 2021-10-17 06:18:04.886263+00:00 Current Datetime in Local Time zone: 2021-10-17 11:48:04.886263+05:30
Summary
We learned how to convert UTC time to the local 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.