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.
Frequently Asked:
- Python: Get difference between two datetimes in hours
- Python : How to convert datetime object to string using datetime.strftime()
- Python: Get difference between two datetimes in microseconds
- Convert timedelta to minutes in Python
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.