Convert Milliseconds into datetime in Python (Example)

 

In this article, I’ll show how to create a datetime object from milliseconds in Python programming.

The article contains the following content:

Let’s just jump right in!

 

Example Data & Add-On Libraries

To be able to use the functions of the datetime module, we first have to import datetime:

import datetime                                              # Load datetime

The following data will be used as a basis for this Python tutorial:

my_ms = 464556556485                                         # Example milliseconds object
print(my_ms)                                                 # Print example data
# 464556556485

The previous Python console output shows that our exemplifying data object contains a certain number of milliseconds.

 

Example: Create datetime Object from Milliseconds Using fromtimestamp() Function

This example demonstrates how to transform milliseconds to a date and time object.

For this task, we can apply the fromtimestamp function as shown below:

my_datetime = datetime.datetime.fromtimestamp(my_ms / 1000)  # Apply fromtimestamp function
print(my_datetime)                                           # Print datetime object
# 1984-09-20 21:29:16.485000

As you can see based on the previous output, we have constructed a new datetime object called my_datetime from our milliseconds object.

 

Video, Further Resources & Summary

Would you like to know more about the construction of a date and time object from milliseconds? Then I can recommend having a look at the following video on my YouTube channel. In the video, I show the Python programming code of this article:

 

The YouTube video will be added soon.

 

Furthermore, you may want to have a look at the other articles on Statistics Globe.

 

In summary: In this post, I have explained how to turn milliseconds into a datetime object in the Python programming language. Tell me about it in the comments below, in case you have further questions. Furthermore, don’t forget to subscribe to my email newsletter to get regular updates on new articles.

 

Matthias Bäuerlen Python Programmer

This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on 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.

Menu
Top