How to Convert DateTime to UNIX Timestamp in Python ? Last Updated : 29 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 number representing seconds passed since the epoch. Let’s explore different method to do this efficientely .Using datetime.timestamp()datetime.timestamp() is a method available on datetime objects in Python. It converts a datetime instance into a POSIX timestamp, which is the number of seconds (including fractions) elapsed since the Unix epoch January 1, 1970, 00:00:00 UTC. Python import datetime dt = datetime.datetime(2021, 7, 26, 21, 20) res = dt.timestamp() print(res) Output1627334400.0 Explanation:datetime.datetime(2021, 7, 26, 21, 20) creates a datetime for July 26, 2021, 9:20 PM.dt.timestamp() converts it to the number of seconds as a float since the Unix epoch (Jan 1, 1970, 00:00 UTC).Using time.mktime()time.mktime() is a function from the time module that takes a time tuple (such as one returned by datetime.timetuple()) and converts it into a Unix timestamp. However, this timestamp is based on the local timezone, not UTC. Python import time import datetime dt = datetime.datetime(2021, 7, 26, 21, 20) res = time.mktime(dt.timetuple()) print(res) Output1627334400.0 Explanation:datetime.datetime(2021, 7, 26, 21, 20) creates a datetime for July 26, 2021, 9:20 PM (local time).dt.timetuple() converts it to a time tuple.time.mktime() converts this local time tuple to a Unix timestamp (seconds since epoch).Using timestamp()You can get the current date and time using datetime.datetime.now(), then convert it to a Unix timestamp in seconds using .timestamp(). Multiplying by 1000 converts seconds into milliseconds, which is useful for higher precision timing or APIs that expect timestamps in milliseconds. Python import datetime now = datetime.datetime.now() res = int(now.timestamp() * 1000) print(res) Output1748514862867 Explanation:datetime.datetime.now() gets the current local date and time..timestamp() converts it to seconds since the Unix epoch (UTC).Multiplying by 1000 converts seconds to milliseconds.int() converts the result to an integer (whole milliseconds).Related ArticlesWorking with Timezones in Python datetimeHow to Format DateTime Objects in Python Comment More infoAdvertise with us Next Article How to convert datetime to date in Python P priyavermaa1198 Follow Improve Article Tags : Python Python-datetime Practice Tags : python Similar Reads Convert Datetime to UTC Timestamp in Python 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 2 min read How to Convert Timestamp to Datetime in MySQL? In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret 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 How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f 3 min read How to Convert a UNIX Timestamp (Seconds Since Epoch) to Ruby DateTime? Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially i 4 min read How to convert DateTime to integer in Python Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it s 2 min read Like