the Date object

Posted on
  • the js Date object is marvelous
    https://www.w3schools.com/jsref/jsref_ob­j_date.asp

    but it's espruino implementation is very limited so far, to be specific, we can only read from the Date object once it has been created.

    >t = new Date;
    =Date { "ms": 949363947471.30676269531 }
    >t.setHours(10);
    Uncaught Error: Function "setHours" not found!
     at line 1 col 3
    

    Im working on a time switch, I've been using date class all around, finding out that set-methods are not implemented made me (almost :P ) depressed

    setDate()   Sets the day of the month of a date object
    setFullYear()   Sets the year of a date object
    setHours()  Sets the hour of a date object
    setMilliseconds()   Sets the milliseconds of a date object
    setMinutes()    Set the minutes of a date object
    setMonth()  Sets the month of a date object
    setSeconds()    Sets the seconds of a date object
    

    Extending espruino's Date class would be extremely beneficial:
    1) easy, date, time manipulation - clocks, timers, time switches
    2) clock module wouldn't be necessary
    3) if it's about memory, we can add set-methods and remove some of these:

    • function Date.getTimezoneOffset - doesn't make sense on espruino since it's always 0;
    • Date.getTime() is almost identical with Date.now() and Date.now() makes more sense on embedded
    • Date.toUTCString is kind of useless when our timezoneOffset is always 0

    Let me know what do you think of it!

  • Thanks - I think on normal Espruino boards it could be added just fine without too many issues with memory. There's actually an issue open for this here: https://github.com/espruino/Espruino/iss­ues/504

    I'll see what I can do - which board are you using?

  • Im using espruino Pico.

    I'm glad there is an open issue for that

    Thanks!

  • Ok, I just added the setters. If you get one of the travis builds from here: https://www.espruino.com/binaries/travis­/master/

    Then you should be able to use them - let me know if they work ok.

  • I've used this one https://www.espruino.com/binaries/travis­/master/espruino_1v92.80_pico_1r3.bin

    works like a charm!

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v92.80 Copyright 2016 G.Williams
    >
    >t = new Date;
    =Date { "ms": 949363217722.27758789062 }
    >t.toString();
    ="Tue Feb 1 2000 00:00:17 GMT+0000"
    >t.setHours(10);
    =949399217722
    >t.toString();
    ="Tue Feb 1 2000 10:00:17 GMT+0000"
    >t.setMinutes(10);
    =949399817722
    >t.toString();
    ="Tue Feb 1 2000 10:10:17 GMT+0000"
    >t.setFullYear(2022);
    =1643710217722
    >t.toString();
    ="Tue Feb 1 2022 10:10:17 GMT+0000"
    >t.setDate(29);
    =1646129417722
    >t.toString();
    ="Tue Mar 1 2022 10:10:17 GMT+0000"
    >t.setMonth(4);
    =1651399817722
    >t.toString();
    ="Sun May 1 2022 10:10:17 GMT+0000"
    > 
    

    Thank you!

  • No problem! If you go for an even more recent build, you'll find that there's an E.setTimeZone(..) function (taking a timezone in hours) that might be interesting for you as well.

  • Yup E.setTimeZone(..) is nice
    anyway I'm on 1v92.86 and I see travis at 1v92.111 now, is there something new worth mentioning? ;)

    by the way, the new Date class with setters is amazing! it simplified my project a lot

  • Great! I don't think you'll gain much by updating at the moment, but you can always check:

    https://github.com/espruino/Espruino/blo­b/master/ChangeLog

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

the Date object

Posted by Avatar for ancienthero @ancienthero

Actions