Open In App

Convert Datetime to UTC Timestamp in Python

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it’s often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this article, we will explore four different methods for converting a DateTime to a UTC timestamp.

Convert A Datetime To A Utc Timestamp In Python

Below, are the ways to Convert A Datetime To A Utc Timestamp In Python.

Convert A Datetime To A Utc Timestamp Using datetime.timestamp()

In this example, the DateTimebelow code creates a datetime object `dt` representing January 25, 2024, 12:00 PM. It then converts this datetime object to a UTC timestamp using the `replace()` method to set the timezone to UTC and the `timestamp()` method to obtain the POSIX timestamp.

Python3




from datetime import datetime, timezone
 
# Create a datetime object
dt = datetime(2024, 1, 25, 12, 0, 0)
 
# Convert to UTC timestamp using timestamp() method
timestamp_utc = dt.replace(tzinfo=timezone.utc).timestamp()
 
print("Datetime:",dt)
print("UTC Timestamp:", timestamp_utc)


Output :

Datetime: 2024-01-25 12:00:00
UTC Timestamp: 1706184000.0

Convert A Datetime To A Utc Timestamp Using calendar.timegm()

In this example, below code utilizes the `calendar` module and `timegm()` function to convert a datetime object `dt` (representing January 25, 2024, 12:00 PM) to a UTC timestamp. It extracts the UTC time tuple from the datetime object using `utctimetuple()` and then employs `timegm()` to calculate the UTC timestamp.

Python3




import calendar
from datetime import datetime
 
# Create a datetime object
dt = datetime(2024, 1, 25, 12, 0, 0)
 
# Convert to UTC timestamp using timegm() function
timestamp_utc = calendar.timegm(dt.utctimetuple())
 
print("Datetime:", dt)
print("UTC Timestamp:", timestamp_utc)


Output :

Datetime: 2024-01-25 12:00:00
UTC Timestamp: 1706184000

Convert A Datetime To A Utc Timestamp Using pytz library

In this example, below code uses the `pytz` library to convert a datetime object `dt` (representing January 25, 2024, 12:00 PM) to a UTC timestamp. It first localizes the datetime object to the UTC timezone using `pytz.utc.localize()`. Then, it obtains the UTC timestamp by converting the localized datetime object using the `timestamp()` method.

Python3




import pytz
from datetime import datetime
 
# Create a datetime object
dt = datetime(2024, 1, 25, 12, 0, 0)
 
# Set the timezone to UTC
dt_utc = pytz.utc.localize(dt)
 
# Convert to UTC timestamp
timestamp_utc = int(dt_utc.timestamp())
 
print("Datetime:", dt)
print("UTC Timestamp:", timestamp_utc)


Output :

Datetime: 2024-01-25 12:00:00
UTC Timestamp: 1706184000

Convert A Datetime To A Utc Timestamp Using time.mktime()

In this example, below code creates a datetime object for January 25, 2024, 12:00 PM, and then converts it to a UTC timestamp using the `time.mktime` function. The original datetime and its corresponding UTC timestamp are printed. This method assumes the datetime object is in the UTC timezone or doesn’t require timezone adjustment before applying `mktime()`.

Python3




import time
from datetime import datetime
 
# Create a datetime object
dt = datetime(2024, 1, 25, 12, 0, 0)
 
# Convert to UTC timestamp using time.mktime
timestamp_utc = int(time.mktime(dt.timetuple()))
 
print("Datetime:", dt)
print("UTC Timestamp:", timestamp_utc)


Output :

Datetime: 2024-01-25 12:00:00
UTC Timestamp: 1706184000

Conclusion

In conclusion, Python offers various methods to convert a datetime object to a UTC timestamp, catering to different preferences and use cases. Whether utilizing built-in functions such as timestamp() or time.mktime, employing libraries like pytz or arrow, or even relying on manual mathematical operations, developers can choose the approach that best fits their coding style and project requirements.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads