Set Unix Timestamp to datetime in Python (Example)

This post demonstrates how to convert a Unix timestamp to a datetime object in the Python.

Create Example Data & Import datetime Module

We can load the datetime module as you can see here:

from datetime import datetime

Then we have to create an exemplifying data object containing a Unix timestamp:

timestamp_x = 1656783467

Example: Set Unix Timestamp to datetime Using fromtimestamp() Function

To transform a Unix timestamp to a datetime object we can apply the fromtimestamp() function as shown in the following code:

datetime_x = datetime.fromtimestamp(timestamp_x)
print(datetime_x)
# 2022-07-02 19:37:47

 

Further Resources

Please find some related tutorials below.

 

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.

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

Menu
Top