Convert datetime to Unix Timestamp in Python (2 Examples)
This tutorial demonstrates how to convert a datetime object to a unix timestamp in the Python programming language.
The following sections are explained in this article:
Let’s start right away!
Import datetime Module & Create Example Data
As a first step, we have to import the datetime module into Python:
import datetime
Next, we can create a datetime object as shown below:
my_datetime = datetime.datetime.now() print(my_datetime) # 2021-11-19 11:59:01.469718
As you can see, we have created a datetime object called my_datetime that contains the date and time 2021-11-19 11:59:01.469718.
Example 1: Convert datetime to Unix Timestamp Using timestamp() Function
This example explains how to convert a datetime object to a unix timestamp using the timestamp() function
To achieve this, we have to multiply the output of the timestamp function by 1000:
my_timestamp1 = my_datetime.timestamp() * 1000 print(my_timestamp1) # 1637323141469.718
Example 2: Convert datetime to Unix Timestamp Using mktime() & timetuple() Functions
This example uses the mktime() and timetuple() functions to transform our datetime to a unix timestamp.
First, we have to import the time module to Python:
import time
Next, we can apply those functions to transform our data. Consider the Python code and its output below:
my_timestamp2 = time.mktime(my_datetime.timetuple()) print(my_timestamp2) # 1637323141.0
Note that the previous timestamp represents our date and time in seconds, not in microseconds as in Example 1
Video, Further Resources & Summary
If you need more explanations on how to change a date and time object to a unix timestamp in Python, have a look at the following video of the PyLenin YouTube channel.
In the video, the speaker explains how to deal with time stamps and unix time zones.
Furthermore, you may have a look at the other Python and datetime tutorials on this website:
- Convert Epoch Time to datetime Object & Vice Versa in Python
- Make Unaware datetime Time Zone Aware in Python
- Convert datetime to Different Time Zone
- Convert datetime Object to Local Time Zone
- All Python Programming Tutorials
The page has shown you how to set the datetime to unix timestamp in the Python programming language. In case you have further questions, please leave a comment below.
Note: This article was created in collaboration with Gottumukkala Sravan Kumar. You may find more information about Gottumukkala and his other articles on his profile page.
Â
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







