Return Timestamp (UNIX) from Current Date & Time in Python (2 Examples)

This article explains how to return the UNIX timestamp from the current date and time in the Python programming language.

Importing Modules to Python

At first, we have to load the time module to the console:

import time         # module time

Example 1: Return Timestamp (UNIX) of Current Date and Time

To extract the UNIX timestamp of the current date and time, we use the time() method:

timestamp_UNIX = time.time()           # Using the time() method
print(timestamp_UNIX)                  # Unix Timestamp
# 1657102781.3507063

UNIX timestamp of the current datetime has been created.

Example 2: Return Timestamp (UNIX) of Current Date and Time Excluding Milliseconds

If you want the UNIX timestamp as an integer (without the milliseconds) you can do the same in addition with the int() function:

timestamp_UNIX_2 = int(time.time())    # Timestamp as Integer
print(timestamp_UNIX_2)                # Unix Timestamp
# 1657102781


Matthias Bäuerlen Python Programmer

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed