Search code examples
javascriptdatechaining

How to chain javascript date functions


Is there a way to chain javascript date functions?

for example, I would like to something like this:

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

this syntax breaks with error:

(new Date).setMinutes(0).setSeconds is not a function

I know I can do this:

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

but this feels verbose and cumbersome. Is there a better way?


Solution

  • You can set seconds and msecs with the setMinutes method:

    var d = new Date();
    d.setMinutes(0,0,0);
    

    Also works with hours- d.setHours(0,0,0,0);