convert javascript Date.now to pandas to_datetime

Issue

I am trying to convert timestamp data that has been fetched with javascript Date.now() and get the date and time into pandas.DataFrame.
I am using pandas.to_datetime to convert the timestamp.

However According to the doc The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

so, converting the timestamp 1636460191573 using pd.to_datetime('1636460191573') give Timestamp('1970-01-01 00:27:16.460191573')

How do I get the date and time from the timestamp in dd/mm/yyyy and hh:mm:sec format?

Solution

Add parameter unit:

timestamp = pd.to_datetime('1636789077923', unit='ms').ceil(freq='s')

print(timestamp.date())
print(timestamp.time())

2021-11-13
07:30:03

Answered By – jezrael

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published