Remove Seconds/ Milliseconds from Date convert to ISO String

A bit late here but now you can:

var date = new Date();

this obj has:

date.setMilliseconds(0);

and

date.setSeconds(0);

then call toISOString() as you do and you will be fine.

No moment or others deps.


There is no need for a library, simply set the seconds and milliseconds to zero and use the built–in toISOString method:

var d = new Date();
d.setSeconds(0,0);
document.write(d.toISOString());

Note: toISOString is not supported by IE 8 and lower, there is a pollyfil on MDN.


A non-library regex to do this:

new Date().toISOString().replace(/.\d+Z$/g, "Z");

This would simply trim down the unnecessary part. Rounding isn't expected with this.


While this is easily solvable with plain JavaScript (see RobG's answer), I wanted to show you the Moment.js solution since you tagged your questions as "momentjs":

moment().seconds(0).milliseconds(0).toISOString();

This gives you the current datetime, without seconds or milliseconds.

Working example: http://jsbin.com/bemalapuyi/edit?html,js,output

From the docs: http://momentjs.com/docs/#/get-set/