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:
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.
Furthermore, you may want to have a look at the other articles on Statistics Globe.
- Convert datetime to Different Time Zone in Python
- Convert datetime Object to Seconds, Minutes & Hours in Python
- Convert String to datetime Object in Python
- Convert datetime Object to Date & Vice Versa in Python
- Convert datetime into String with Milliseconds in Python
- Python Programming Language
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.
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.