Current Date

Posted on
  • Hi

    How do you achieve the current date and time? I have tried new Date(); but like this:

    Thu Jan 1 1970 02:43:17 GMT+0000

    Thanks...

  • Espruino has a real time clock which accurately records the elapsed time since the device was started (powered up). This can be retrieved in ms using getTime().

    There is also a Date class which handles dates & times (calendar, years, months, days, hours, mins etc.)

    These are tied together using the Clock class to manage the current date/time. An instance of this class needs to be instantiated/initialised every time the device is restarted as shown below:

      var Clock = require("clock").Clock;
      var clk=new Clock(2014,4,15,23,45,0,0);   // Initialise with specific date
      var clk=new Clock("Jun 23, 2014 12:18:02");   // ... or Initialise with specific date
    

    You can then retrieve the current date/time as follows:

        var currentDatetime = clk.getDate(); 
    

    See http://www.espruino.com/Reference#Date , http://www.espruino.com/clock and http://www.espruino.com/Clocks for more details.

  • Ok so I will have to provide the current date & time at start up as a kind of calibration? With WiFi one can use a time provider I guess...

    Thank you

  • Yes - that is exactly what I was planning to do.

    Numerous simple services exist e.g.

    http://www.timeapi.org/utc/now
    Or
    http://currentmillis.com/api/millis-sincĀ­e-unix-epoch.php

    This should be easily integrated using http.get() see:

    http://www.espruino.com/Internet

  • When you request a webpage from a webserver, the headers contain the current date and time - which can be parsed directly by Date - that should make getting the time much easier.

    1v71 will have some changes so that it remembers the current time after a hard reset (only forgetting it when powered off). That should make keeping time a lot easier, as assuming you keep a battery connected it'll always remember the correct time once you've programmed it,

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

Current Date

Posted by Avatar for Espruino_user_dk @Espruino_user_dk

Actions