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 |
from datetime import datetime
Then we have to create an exemplifying data object containing a Unix timestamp:
timestamp_x = 1656783467 |
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 |
datetime_x = datetime.fromtimestamp(timestamp_x) print(datetime_x) # 2022-07-02 19:37:47
Further Resources
Please find some related tutorials below.
- Get Time Difference Between Two Dates & Times in Python (datetime Module)
- Set datetime to Different Time Zone in Python (3 Examples)
- Set datetime to Unix Timestamp in Python (2 Examples)
- All Python Programming Tutorials
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.