How to Get UTC Timezone in Python?

Here, we see how to retrieve UTC timezone time in Python. For this, we use datetime library in python. We utilize the pytz module to get the UTC time. The datetime module can assist you in determining the date and time.

What is UTC Time?

UTC is an abbreviation for Coordinated Universal Time, which is the time standard used to establish all time zones around the world. So, for example, New York City is in the UTC minus five timezone, which means that it is 5 hours earlier in NYC than the reading on a UTC clock (except during U.S. daylight savings, when it is 4 hours earlier).

The concept of universal time was first proposed in the late 1800s, when rail and shipping lines linked the world and standard timetables were required to coordinate commercial activity. Previously, the time of day was determined by what the clocks in that particular location said.

However, local times can fluctuate by seconds or even minutes – an unpleasant situation if you’re running late and want to know if you’ll reach it to the station in time.

How to Get UTC Time in Python?

Example1

Approach:

  • Import datetime module using the import keyword.
  • Get the current date by calling the today() function of the datetime module.
  • Store it in a variable.
  • Get the current date and time by calling the now() function of the datetime module.
  • Store it in another variable.
  • Print the current date.
  • Print the current date and time.
  • The Exit of the Program.

Below is the implementation:

# Import datetime module using the import keyword.
import datetime

# Get the current date by calling the today() function of the datetime module
# Store it in a variable
current_date = datetime.date.today()
# Get the current date and time by calling the now() function of the datetime module
# Store it in another variable
current_time = datetime.datetime.now()
# Print the current date
print("The current date:", current_date)
# Print the current date and time
print("The current date and time:", current_time)

Output:

The current date: 2022-03-07
The current date and time: 2022-03-07 20:34:54.934896

Now to convert the current time to UTC timezone we will use the pytz (python time zone) module

Example2

Approach:

  • Import pytz module using the import keyword.
  • Import datetime module using the import keyword.
  • Get the current date and time by calling the now() function of the datetime module
  • Store it in a variable.
  • Print the current date and time
  • Convert the current time to UTC timezone using the pytz.utc by passing it as an argument to the now() function.
  • Store it in another variable
  • Print the UTC timezone for the current time.
  • The Exit of the Program.

Below is the implementation:

# Import pytz module using the import keyword.
import pytz
# Import datetime module using the import keyword.
import datetime

# Get the current date and time by calling the now() function of the datetime module
# Store it in a variable
current_datetime = datetime.datetime.now()

# Print the current date and time
print("The current date and time:", current_datetime)
# Convert the current time to UTC timezone using the pytz.utc by passing it as an 
# argument to the now() function
# Store it in a variable
utc_time = datetime.datetime.now(pytz.utc)
# Print the UTC timezone for the current time.
print("The UTC timezone:", utc_time)

Output:

The current date and time: 2022-03-07 20:46:08.033294
The UTC timezone: 2022-03-07 20:46:08.035301+00:00