To generate a Python datetime object from milliseconds, you can use the datetime.fromtimestamp()
method. However, since the fromtimestamp()
method takes seconds as input, you need to divide the milliseconds by 1000 first. Here is an example:
import datetime
milliseconds = 1578744600387
seconds = milliseconds / 1000.0
dt = datetime.datetime.fromtimestamp(seconds)
print(dt)
Output:
2020-01-11 18:50:00.387000
Alternatively, you can use the datetime.utcfromtimestamp()
method if you want the datetime object to be in UTC timezone:
import datetime
milliseconds = 1578744600387
seconds = milliseconds / 1000.0
dt = datetime.datetime.utcfromtimestamp(seconds)
print(dt)
Output:
2020-01-11 11:50:00.387000
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-03 11:00:00 +0000
Seen: 7 times
Last updated: May 01 '22
How can I set up Gunicorn with a Django Project?
Looking for a Python Module that finds Tags for a Text describing its Content
Need a Function in Python to remove entries less than 2 digits from an Array
How can I convert a Document in Python?
How can I program a Loop in Python?
How can I enable Python Code Highlighting in Askbot?