![]() |
VOOZH | about |
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:
Current timestamp (integer): 1744234690
Explanation:
datetime_object.timestamp()This example shows how to get the current local time and convert it into an integer timestamp in seconds.
Current datetime: 2025-04-10 11:41:17.312551 Integer timestamp of current datetime: 1744285277
Explanation:
Here we define a specific date and time and convert it to both seconds and milliseconds timestamps.
Integer timestamp in seconds: 1514836800 Integer timestamp in milliseconds: 1514836800000
Explanation:
This example shows how to get the UTC integer timestamp using calendar.timegm() and timetuple().
UTC timestamp (integer): 7260
Explanation:
This example demonstrates how to create a timezone-aware datetime and convert it to an integer timestamp using pytz.
Time Zone: Asia/Kolkata Datetime: 2025-04-09 21:48:07.468073+05:30 Integer timestamp: 1744215487
Explanation: