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 how to convert a Python datetime object into an integer timestamp-either in seconds or milliseconds. Let's take a quick look at how we can convert the current date and time into an integer timestamp:
from datetime import datetime
now = datetime.now()
ts = int(now.timestamp())
print("Current timestamp (integer):", ts)
Output
Current timestamp (integer): 1744234690
Explanation:
- datetime.now() fetches the current local date and time.
- .timestamp() converts the datetime to the number of seconds since epoch (as a float).
- int() converts the float to an integer timestamp.
Syntax
datetime_object.timestamp()
- Returns a float value: number of seconds since the epoch.
- Wrap with int() or round() to get a clean integer timestamp.
Examples
Example 1: Current Datetime to Integer Timestamp
This example shows how to get the current local time and convert it into an integer timestamp in seconds.
from datetime import datetime
curr = datetime.now()
print("Current datetime: ", curr)
t = int(round(curr.timestamp()))
print("Integer timestamp of current datetime: ", t)
Output
Current datetime: 2025-04-10 11:41:17.312551 Integer timestamp of current datetime: 1744285277
Explanation:
- datetime.now() gets the current date and time.
- .timestamp() converts it to seconds since epoch.
- round() makes it precise, and int() ensures it's a whole number.
Example 2: Specified Datetime to Integer & Millisecond Timestamp
Here we define a specific date and time and convert it to both seconds and milliseconds timestamps.
from datetime import datetime
dt = datetime(2018, 1, 1, 20)
dts = dt.timestamp()
s = int(round(dts))
print("Integer timestamp in seconds:",s)
ms = int(round(dt.timestamp() * 1000))
print("Integer timestamp in milliseconds:", ms)
Output
Integer timestamp in seconds: 1514836800 Integer timestamp in milliseconds: 1514836800000
Explanation:
- We create a datetime object for a specific date and time.
- .timestamp() returns a float (e.g., 1514817000.0)
- Multiply by 1000 to get milliseconds.
Example 3: UTC Timestamp using calendar.timegm()
This example shows how to get the UTC integer timestamp using calendar.timegm() and timetuple().
import datetime
import calendar
d = datetime.datetime(1970, 1, 1, 2, 1, 0)
t = d.timetuple()
its = calendar.timegm(t)
print("UTC timestamp (integer):", its)
Output
UTC timestamp (integer): 7260
Explanation:
- .timetuple() breaks the datetime into a tuple.
- calendar.timegm() turns that tuple into a UTC timestamp.
- This is great when you're working with timezone-naive UTC data.
Example 4: Integer Timestamp with Timezone using pytz
This example demonstrates how to create a timezone-aware datetime and convert it to an integer timestamp using pytz.
import datetime
import pytz
dt = datetime.datetime.now()
t = pytz.timezone("Asia/Kolkata")
dtz = t.localize(dt)
print("Time Zone:", dtz.tzinfo)
print("Datetime:", dtz)
tstamp = dtz.timestamp()
print("Integer timestamp:", int(round(tstamp)))
Output
Time Zone: Asia/Kolkata Datetime: 2025-04-09 21:48:07.468073+05:30 Integer timestamp: 1744215487
Explanation:
- We use pytz to handle the timezone (Asia/Kolkata in this case).
- .localize() attaches the timezone info to the datetime object.
- Then we get the localized timestamp and convert it to an integer.