Convert Unix Timestamp to datetime in Python (Example)
In this article I’ll demonstrate how to convert a Unix timestamp to a datetime object in the Python programming language.
Table of content:
Let’s dive into it!
Import datetime Module & Create Example Data
First, we have to import the datetime module to Python:
from datetime import datetime
Next, we have to create an exemplifying data object containing a Unix timestamp:
my_timestamp = 1534561289
Looks good! Let’s convert this timestamp to a datetime object…
Example: Convert Unix Timestamp to datetime Using fromtimestamp() Function
This example converts a Unix timestamp to a datetime object in Python.
For this task, we can apply the fromtimestamp() function as shown below:
my_datetime = datetime.fromtimestamp(my_timestamp) print(my_datetime) # 2018-08-18 05:01:29
As you can see, we have converted out Unix timestamp to a datetime object.
Video, Further Resources & Summary
Are you searching for further explanations and examples on how to transform a Unix timestamp to a date and time object in Python? Then you should have a look at the following YouTube video of the YouTube channel of Pretty Printed.
In the video, he demonstrates how to format and parse datetime objects in the Python programming language:
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 to the video, you could have a look at some of the other tutorials on Statistics Globe:
This post has shown how to switch from a Unix timestamp to a datetime object in the Python programming language. If you have any further questions, you may leave a comment below.
This tutorial was created in collaboration with Gottumukkala Sravan Kumar. Please have a look at Sravan’s author page to get more information about his academic background and the other articles he has written for Statistics Globe.