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 |
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 print(x) # 2001-04-21 13:27:45.468000 |
x = datetime.datetime.fromtimestamp(987852465468 / 1000.0) # Using fromtimestamp function print(x) # 2001-04-21 13:27:45.468000
Further Resources
Please find some related tutorials below.
- Set Unix Timestamp to datetime in Python (Example)
- Set String to datetime Object in Python (3 Examples)
- Transform datetime into String with Milliseconds in Python (3 Examples)
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.