ziggy ziggy - 1 month ago 11
Python Question

Python convert timestampMS to date

using python 2.7 I have timestampMS ex:

timestampMs 1478001612891


I want to convert it to a date format.

I have tried
datetime.fromtimestamp(int('1478013706323'))


but get this error

Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: timestamp out of range for platform localtime()/gmtime() function

Answer

datetime.fromtimestamp expects input in seconds. Divide your number by 1000.0 before passing it in.

(From the specifications: datetime.fromtimestamp(timestamp) will return the local date corresponding to the POSIX timestamp, such as is returned by time.time(). And time.time() will return the time in seconds since the epoch as a floating point number.)