Create datetime Object from Milliseconds in Python (Example Code)

In this article, I’ll illustrate how to construct a date and time object from milliseconds in the Python programming language.

Example: Apply fromtimestamp() Function to Transform Milliseconds into datetime Object

Before we start with our example, we first have to load the datetime module:

import datetime                                             # Load datetime

Now we can use the fromtimestamp() function from the datetime module to create a datetime object from a milliseconds input:

x = datetime.datetime.fromtimestamp(987852465468 / 1000.0)  # Using fromtimestamp function
# 2001-04-21 13:27:45.468000

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

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