javascript time stamp to python date

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

javascript time stamp to python date

Post by devnull » Wed Nov 07, 2018 3:27 am

I am getting a timestamp in miliseconds via json from javascript nodejs, I cannot change this format.

When I try and convert this to a python date, it is 30 years in the future:

Code: Select all

>>> import utime
>>> jsmsec = 1541559855359
>>> jssecs = int(jsmsec/1000)
>>> utime.localtime(jssecs)
(2048, 11, 6, 3, 3, 28, 4, 311)
However this is not as easy as just deducting 60 x 60 x 24 x 365 x 30 as there are leap years to consider.

Is there a simple way of converting a numeric javascript time in miliseconds to python date ?

chuckbook
Posts: 135
Joined: Fri Oct 30, 2015 11:55 pm

Re: javascript time stamp to python date

Post by chuckbook » Wed Nov 07, 2018 6:46 pm

Use:

Code: Select all

UNIX_TIME = 946681200
mpy_ts = js_ts - UNIX_TIME*1000

Post Reply