Convert Datetime to UTC Timestamp in Python
Last Updated :
01 Jul, 2025
Converting a Datetime to a UTC Timestamp in Python means transforming a date and time into the number of seconds since the Unix epoch (January 1, 1970, UTC), while correctly accounting for time zones. For example, converting January 25, 2024, 12:00 PM UTC to a timestamp would yield 1706184000.0. Let's explore different methods to do this efficiently.
Using datetime.timestamp()
This method attaches the UTC timezone to a naive datetime object using replace(tzinfo=timezone.utc) and then calls .timestamp() to get the UTC timestamp. This is the most recommended and modern approach for converting to a UTC timestamp.
Python
from datetime import datetime, timezone
dt = datetime(2024, 1, 25, 12, 0, 0)
res = dt.replace(tzinfo=timezone.utc).timestamp()
print(res)
Explanation: A naive datetime for January 25, 2024, at 12:00 PM is created without timezone info. replace(tzinfo=timezone.utc) makes it UTC-aware and .timestamp() returns the correct Unix timestamp.
Using calendar.timegm()
In this method, the datetime is converted to a UTC time tuple using .utctimetuple() and then calendar.timegm() computes the timestamp. It’s best suited for naive datetime objects that represent UTC.
Python
import calendar
from datetime import datetime
dt = datetime(2024, 1, 25, 12, 0, 0)
res = calendar.timegm(dt.utctimetuple())
print(res)
Explanation: A naive datetime for January 25, 2024, 12:00 PM is treated as UTC. utctimetuple() converts it to a UTC tuple and calendar.timegm() returns the Unix timestamp.
Using pytz.utc.localize()
This method uses the pytz library to localize a naive datetime to UTC, then converts it using .timestamp(). It's useful for multi-timezone applications and ensures accurate timezone handling, though it requires an external library.
Python
import pytz
from datetime import datetime
dt = datetime(2024, 1, 25, 12, 0, 0)
dt_utc = pytz.utc.localize(dt)
res = dt_utc.timestamp()
print(res)
Explanation: A naive datetime for January 25, 2024, at 12:00 PM is created. pytz.utc.localize(dt) makes it UTC-aware and .timestamp() returns the seconds since the Unix epoch.
Using time.mktime()
time.mktime() method converts a datetime to a timestamp based on local time, not UTC. It’s simple and uses dt.timetuple(), but isn't reliable for UTC conversions and should be used only for local timestamps.
Python
import time
from datetime import datetime
dt = datetime(2024, 1, 25, 12, 0, 0)
res = time.mktime(dt.timetuple())
print(res)
Explanation: dt.timetuple() creates a time tuple and time.mktime() returns the timestamp based on local time. Though simple and built-in, it’s unreliable for UTC conversions if local time differs from UTC.
Related articles
Similar Reads
How to Convert DateTime to UNIX Timestamp in Python ? Generating a UNIX timestamp from a DateTime object in Python involves converting a date and time representation into the number of seconds elapsed since January 1, 1970 (known as the Unix epoch). For example, given a DateTime representing May 29, 2025, 15:30, the UNIX timestamp is the floating-point
2 min read
How to convert NumPy datetime64 to Timestamp? In this article, we will discuss how to convert NumPy datetime64 to Timestamp in Python.Example:Â Input: If the current datetime64 is as follows: 2020-08-18 09:31:51.944622 Output: The required timestamp in seconds will be: 1597743111.944622 The required timestamp in minutes will be: 26629051.865743
2 min read
Convert string to datetime in Python with timezone Converting a string to a datetime in Python with timezone means parsing a date-time string and creating a timezone-aware datetime object. For example, a string like '2021-09-01 15:27:05.004573 +0530' can be converted to a Python datetime object that accurately represents the date, time and timezone.
2 min read
Datetime to integer timestamp in Python A timestamp represents the number of seconds that have passed since January 1, 1970, 00:00:00 UTC (also known as the epoch). We can convert a datetime object to a timestamp using the built-in timestamp() method, and then round or typecast it to get an integer version. In this article, we'll learn ho
3 min read
Python | Pandas Timestamp.to_pydatetime Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.to_pydatetime() function convert a Timestamp object to a native Pytho
2 min read