Search code examples
pythondatetimetimestampunix-timestamp

Convert the metric timestamp to a human date


I am trying to collect some data from Dynamics App, and I would like to convert the metric timestamp to a human script, but I am getting this error, any suggestions for a solution ?

Script

from datetime import datetime

timestamp = 1630548180000
dt_object = datetime.fromtimestamp(timestamp)

Output

Traceback (most recent call last):
  File "C:\Users\Developer\Desktop\test.py", line 4, in <module>
    dt_object = datetime.fromtimestamp(timestamp)
OSError: [Errno 22] Invalid argument

Solution

  • Python datetime expects epoch seconds, but it seems you have timestamp of milliseconds, so you need to divide this value by 1000.