Get UNIX Timestamp of Current Date & Time in Python (2 Examples)
On this page, I’ll illustrate how to return the UNIX timestamp from the current date and time in the Python programming language.
Table of contents:
Loading Time Module
Before we can start, we have to load the time module to the Python console:
import time # Load time module
Let’s dive right into the examples…
Example 1: Get Unix Timestamp of Current Date & Time
To return the current datetime as Unix timestamp, we can use the time function, as you can see in the following Python syntax:
unix_timestamp = time.time() # Applying time() method print(unix_timestamp) # Return Unix Timestamp # 1657100718.050852
The output returns us the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), in our case 1657100718.050852
Example 2: Get Unix Timestamp of Current Date & Time Without Milliseconds
As you can see in the first example, the milliseconds are also included. If we want to print the current Unix timestamp without milliseconds, we can use the int() function, which will return us the result as integer:
unix_timestamp_2 = int(time.time()) # time() & int() function print(unix_timestamp_2) # Show Unix Timestamp # 1657100718
Looks good, now the millisecond component disappeared from the Unix timestamp.
Video, Further Resources & Summary
Do you need more explanations on the topics of the present article? Then you might watch the following video instruction on my YouTube channel. I demonstrate the topics of this tutorial in the video:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may read the related tutorials on my website. I have published numerous tutorials already:
- Get Current Time in Specific Timezone in Python
- Get Current Date & Time in Python
- Python Programming Language
Summary: At this point you should have learned how to extract the UNIX timestamp of the current date and time in the Python programming language. Let me know in the comments below, in case you have further questions. Furthermore, don’t forget to subscribe to my email newsletter in order to receive regular updates on new articles.
This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.