Date confusion

Posted on
  • i am working with dates and got some confusing behaviour

    >console.log(new Date(getTime()).toString());
    Sun Jan 18 1970 00:24:12 GMT+0000
    >console.log(new Date().toString());
    Wed Aug 3 2016 19:29:50 GMT+0000
    

    i expected to be able to create a new Date with the value from getTime()
    getTime() returns a floating point number, but with rounding Math.round(getTime()); the result does not change

    Is there some error in my code or my expectations, maybe some misinterpretation of the docs on my side?

  • nope.... (no problem in your code) - may be... (in interpretation of doc)...

    
    On power on - py Pico shows 949363215.70712661743 seconds for ```getTime()``` (**Loading 2527 bytes from flash...** is form ```reset()``` and ```save()```).
    
    

    Connected
    Loading 2620 bytes from flash...
    new Date().toString()
    ="Tue Feb 1 2000 00:00:18 GMT+0000"
    getTime()
    =949363232.21488475799

    
    ... after uploading, it shows more 'now': 1470274624.28208160400
    
    

    | |_ ___ ___ _ ||___ ___
    | |_ -| . | _| | | | | . |
    |
    || || |_|||_|_|

          |_| http://espruino.com
    

    1v86 Copyright 2016 G.Williams
    echo(0);
    =undefined
    new Date().toString()
    ="Thu Aug 4 2016 01:42:42 GMT+0000"
    getTime()
    =1470274974.15566730499

    ```

    Initially, there was no synchronization between time of your Browser/Web IDE and time of Espruino. Today, when uploading code (even empty editor pane), time is set to GMT+0000 (UTC) from the Browser/Web IDE has. Date() 'class'/object has always GMT+0000 / UTC. If Espruino connected to, for example, Web / http or a GPS, you may pull time from there.

    You can set the time by taking the [ms] since "Thu, 01 Jan 1970 00:00:00 GMT" - in Browser Inspector Console with new Datae().getTime() - and divide them by 1000 to get seconds and feed those into setTime(seconds) in Espruino console. This will set your Espruino time to now in GMT+0000 / UTC.

    This conversation 'fools' around with date and time formatting.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Date confusion

Posted by Avatar for MichaelPralow @MichaelPralow

Actions