Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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