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)
# 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:



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.


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. Required fields are marked *

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