Working with DateTime

Posted on
  • Hi,

    I'm a little confused...
    Is 'new Date()' supposed to return the current DateTime like 'Thu Nov 07 2019 17:36:22 GMT+0200'?
    Somehow I'm getting 'Thu Jan 1 1970 02:46:57 GMT+0000'

    Am I missing something?


  • There is a checkbox in IDE settings that seeds date and time from your IDE machine on code upload to Espruino board.

  • Ok, I can now see the correct date and time, but, my tests shows that if I'm using onInite function when the board is connected to an external power source the date goes back to 1970, is that mean that I need to manage getting the current datetime from the internet or whatever so the board can continue from there or there is another better solution for boards that does not connect to the internet like Puck.js etc. ?


  • Yes, removing power from it resets the clock.
    If you keep the Puck powered, it will remember the time. And will last a long time on battery.
    Or you can use an external RTC with it's own battery, and read time from the RTC on startup.
    Or you can set the time with setTime when you connect to it. You can do it automatically with some coding, if you connect to it with BLE from your computer of phone.

  • If you're worried about losing the time, the best bet is to ensure that you have some backup battery always connected to the device via a diode.

  • I'd like to use my Pico as a temperature logger. The script's working, but how do I now set the time and keep it from resetting?

    I am planning to use 4xAA Eneloops as the power source. Do I just keep them connected and connect IDE also via USB to set the time?

    I'd like to add some indication that time needs setting, like flash a led. But how should I check that time is set correctly - just some lower bound as a sanity check?

  • @TeppoKurki,

    I guess you figure when connected - and powered over USB - you can make the upload of code to set the time - an option in the ide-settings... Unfortunately, as far as I know, any power cycle loses the time.

    You can power the pico from the batteries over the designated connector, then connect to usb, then upload the code into RAM with time setting - do not start your code / application yet, but have an onInit() function which includes the start of your code / application. While still connected, you can write the current time to Storage with function you call from console or with statements executed in the console. In your code, your read that time and compare it to the time stored - your hinted lower bound as a sanity check. If the stored time is ahead, you obviously got a power cycle and lost your time and you can signal that. You reconnect and you can set the time with some js statements in the console.

    To resolve time for good, get yourself a simple RTC clock module w/ battery backup - either Dallas DS1302 or DB3231. (I assume you had planned to have nothing else connected to Esuprino except the batteries and temp sensor(s)).

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

Working with DateTime

Posted by Avatar for michael_101 @michael_101