Date Time Format Unknown

Viewed 84

I am trying to figure out a time format used by someone else. In addition to the date, I have time with an example being the following: 1641859200000

I cant seem to figure out what time or date time format this is. It cannot be HHMMSS, because in this example the second is 85, which is not possible. Any idea what format this is, and how I can convert it using Python to HH:MM:SS ?

Thank you :)

4 Answers

You have a timestamp in seconds from Epoch since January 1, 1970 (midnight UTC/GMT). To convert to a datetime, use:

from datetime import datetime
print(datetime.fromtimestamp(1641859200000 / 1000))

# Output
2022-01-11 01:00:00

Note: you have to divide by 1000 because this timestamp contains milliseconds and Epoch should be in seconds.

This is a Unix-Timestamp.

You can convert in a human-readable format like this:

from datetime import datetime

timestamp = 1641859200000/1000
dt = datetime.fromtimestamp(timestamp)

print(dt)

Edit: didn't check the actual timestamp, whyever, this has to be divided by 1000 as done in the othe answer.

This probably is a unix timestamp https://en.wikipedia.org/wiki/Unix_time. The factor 1000 stems from a millisecond representation I think. Depends on, from where you got this stamp.

You can convert it using:

>>> datetime.datetime.fromtimestamp(1641859200000/1000)
datetime.datetime(2022, 1, 11, 1, 0)

Take a look at dateparser https://dateparser.readthedocs.io/. It will help you figure out what the date and time is based on the range of input date strings:

pip install dateparser
>>> import dateparser
>>> dateparser.parse('1641859200000')
datetime.datetime(2022, 1, 11, 1, 0)

Your timestamp is miliseconds since Unix epoch in this case but if you ever run into similar problem dateparser could help you.


Regarding the second part of the question. Convertion to HH:MM:SS format

>> dt = datetime.datetime(2022, 1, 11, 1, 0)
>> dt.strftime("%H:%M:%S")
'01:00:00'

Additional info: Available Format Codes

Related