Date class setters

Posted on
  • Hello again,

    I experienced that the Date class has no setters like:

    Date.setHours(Number);
    Date.setMinutes(Number);
    Date.setSeconds(Number);
    ...
    

    How can I create a new Date() object for example for today, but time of day 12:00 for example?

    In regular JavaScript I write something like this:

    var d = new Date();
    d.setHours(12);
    d.setMinutes(0);
    d.setSeconds(0);
    

    In Espruino this doesn't work :(

    Best,

    Tobias

  • A workaround could be:

    var date = new Date();
    var future = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0);
    console.log(future.toString());
    

    Best

    Tobias

  • Hi - yes, at the moment that's what you'd have to do I'm afraid.

    I've filed a bug for this though - so at some point I will add the setters. It'd make life a lot easier!

  • Great, thank you Gordon!

  • I've been playing with the date functions because I am trying to build an IP connected clock but I am having a nightmare. Probably the fact that I don't normally code but the references don't seem to work very well in practical applications. Once I've figured things out I should share what I've found but I don't have much time to work on it.

  • references don't seem to work very well in practical applications.

    What do you mean by that?

  • Espruino - Pico user:
    As a beginner I want to ask about the following construction (taken from MDM)...
    var birthday = new Date('December 17, 1995 03:24:00');
    But console.log( birthday.toString()); results in Thu Jan 1 1970 00:00:00 GMT+0000.
    Is the contruction with 'December 17, 1995 03:24:00' not possible?

  • Looks like Dec 17, 1995 03:24:00 works but December 17, 1995 03:24:00 doesn't.

    I've just fixed this so it'll be in 1v81 of Espruino. Looks like JavaScript only really checks the first 3 characters, so for instance this should also work: (new Date('Decfdsm 17, 1995 03:24:00')).toString()

  • Thank you for the fast response :-)

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

Date class setters

Posted by Avatar for net-tobi @net-tobi

Actions