How to output date in javascript in ISO 8601 without milliseconds and with Z

bessarabov picture bessarabov · Dec 2, 2015 · Viewed 57.2k times · Source

Here is a standard way to serialise date as ISO 8601 string in JavaScript:

var now = new Date();
console.log( now.toISOString() );
// outputs '2015-12-02T21:45:22.279Z'

I need just the same output, but without milliseconds. How can I output 2015-12-02T21:45:22Z

Answer

Blue Eyed Behemoth picture Blue Eyed Behemoth · Dec 2, 2015

Simple way:

console.log( now.toISOString().split('.')[0]+"Z" );