Get current UTC time in Python

This article will discuss different ways to get the current time in UTC format in python.

Get current time in UTC format using datetime.now()

Suppose you are not in a UTC timezone, but you only want the current time in the UTC timezone. For that, you can use the datetime module’s now() function. In the now() function, you can pass the timezone information, and it returns the current time in that timezone.

So, to get the current date and time, we can call the now() function with the UTC timezone. For example,

from datetime import datetime, tzinfo
import pytz

# Get current datetime in UTC
utc_now_dt = datetime.now(tz=pytz.UTC)

print('Current Datetime in UTC: ', utc_now_dt)

Output:

Current Datetime in UTC:  2021-10-15 12:25:01.784908+00:00

It returned the timezone aware datetime object, which contains the current time in the UTC timezone.
Instead of datetime, if you want the current time in UTC as a string, we can call the strftime() function on the datetime object with a format string, and it will give us the current date & time as a string but in UTC timezone. For example,

from datetime import datetime
import pytz

# Get current UTC time in MM-DD-YYYY HH:MM:SS format string
current_time_str = datetime.now(tz=pytz.UTC).strftime("%m/%d/%Y, %H:%M:%S")

print(current_time_str)

Output:

10/15/2021, 12:26:31

Get current time in UTC format using datetime.utcnow()

In the previous example, we got the current UTC time as a datetime object. Also, the datetime object had the UTC timezone associated with it. If you want the current UTC time as a datetime, but without the associated timezone information. For that, you can use the utcnow() function of the datetime module. It returns a datetime object containing the current time in UTC timezone, but the timezone information in this datetime object will be null. For example,

from datetime import datetime

# Get current datetime in UTC
utc_now_dt = datetime.utcnow()

print('Current Datetime in UTC: ', utc_now_dt)
print('tzinfo: ', utc_now_dt.tzinfo)

Output:

Current Datetime in UTC:  2021-10-15 12:26:31.874155
tzinfo:  None

It returned a naive datetime i.e., with no timezone information. Therefore datetime object had the current time in the UTC timezone, but associated tzinfo was None.

Instead of datetime, if you want the current time in UTC as a string, we can call the strftime() function on the datetime object with a format string, and it will give us the current UTC time in string. For example,

from datetime import datetime

# Get current UTC time in MM-DD-YYYY HH:MM:SS format string
current_time_str = datetime.utcnow().strftime("%m/%d/%Y, %H:%M:%S")

print(current_time_str)

Output:

10/15/2021, 12:32:02

Summary:

We learned different ways to get the current time in the UTC timezone.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top