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

6

128

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

Azerbaijani answered 2/12, 2015 at 21:49 Comment(1)
(new Date).toISOString().replace(/\.\d+/, "").Suspect
A
195

Simple way:

console.log( new Date().toISOString().split('.')[0]+"Z" );
Aerodontia answered 2/12, 2015 at 21:54 Comment(2)
BTW, this also works for getting just the date with a slight modification: new Date().toISOString().split('T')[0]Suribachi
Please use (new Date) instead of the ugly new Date().Suspect
A
41

Use slice to remove the undesired part

var now = new Date();
alert( now.toISOString().slice(0,-5)+"Z");
Arun answered 2/12, 2015 at 21:55 Comment(3)
This should be the accepted answer because it's 10x faster than the solutions using split() and 3x faster than the regex solution. The only (unlisted) solution of equal speed is now.toISOString().substr(0,19) + "Z". And extra speed is helpful when using this with large data sets. Up voted.Croix
@Roberto If I was looking for speed, I would use console.log( now.substring(0, now.indexOf('.'))+"Z"); since toISOString() is 24 or 27 characters long. (now = new Date().toISOString(); in my example)Aerodontia
For those who are debating whether to .slice(0,-5) vs. .substring(0,19), even at 0 milliseconds, ".000Z" will be returned.Piazza
T
21

This is the solution:

var now = new Date(); 
var str = now.toISOString();
var res = str.replace(/\.[0-9]{3}/, '');
alert(res);

Finds the . (dot) and removes 3 characters.

http://jsfiddle.net/boglab/wzudeyxL/7/

Tuft answered 2/12, 2015 at 21:59 Comment(1)
Surprised me that this is actually faster than the solutions using split. And it also avoids having to add back the Z. Up voted, though the slice and substr solutions are slightly better.Croix
T
11

You can use a combination of split() and shift() to remove the milliseconds from an ISO 8601 string:

let date = new Date().toISOString().split('.').shift() + 'Z';

console.log(date);
Trenna answered 3/8, 2018 at 18:29 Comment(1)
I think shift() might be overkill especially if you're just trying to get the first value in the array. It's just extra overhead.Aerodontia
B
6

It is similar to @STORM's answer:

const date = new Date();

console.log(date.toISOString());
console.log(date.toISOString().replace(/[.]\d+/, ''));
Biltong answered 30/11, 2020 at 15:9 Comment(2)
/\.\d+/ is also possible.Suspect
the \. can be difficult to read sometimes, so I have changed into using [.]Biltong
A
2

or probably overwrite it with this? (this is a modified polyfill from here)

function pad(number) {
  if (number < 10) {
    return '0' + number;
  }
  return number;
}

Date.prototype.toISOStringShort = function() {
  return this.getUTCFullYear() +
    '-' + pad(this.getUTCMonth() + 1) +
    '-' + pad(this.getUTCDate()) +
    'T' + pad(this.getUTCHours()) +
    ':' + pad(this.getUTCMinutes()) +
    ':' + pad(this.getUTCSeconds()) +
    'Z';
};
Aam answered 23/11, 2016 at 19:53 Comment(2)
Not a bad idea but the person probably down voted because of the dangers of overwriting the prototype! Extend it instead and call your method something like Date.prototype.toISOStringSansMilliseconds or some suchKirsti
Please never change builtin methods.Eruption

© 2022 - 2024 — McMap. All rights reserved.