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:

 

The YouTube video will be added soon.

 

In addition, you may read the related tutorials on my website. I have published numerous tutorials already:

 

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.

 

Matthias Bäuerlen Python Programmer

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.

 

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.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top