117

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

1

6 Answers 6

183

Simple way:

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

2
  • 11
    BTW, this also works for getting just the date with a slight modification: new Date().toISOString().split('T')[0]
    – krowe2
    Sep 10, 2018 at 20:43
  • 1
    Please use (new Date) instead of the ugly new Date(). May 28, 2022 at 22:23
33

Use slice to remove the undesired part

var now = new Date();
alert( now.toISOString().slice(0,-5)+"Z");
3
  • 9
    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.
    – Yogi
    Dec 8, 2019 at 6:17
  • @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) Jul 10, 2020 at 1:04
  • For those who are debating whether to .slice(0,-5) vs. .substring(0,19), even at 0 milliseconds, ".000Z" will be returned.
    – Tranzium
    Sep 6, 2022 at 18:55
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/

1
  • 2
    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.
    – Yogi
    Dec 8, 2019 at 6:24
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);

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. Sep 13, 2018 at 15:09
6

It is similar to @STORM's answer:

const date = new Date();

console.log(date.toISOString());
console.log(date.toISOString().replace(/[.]\d+/, ''));

2
  • /\.\d+/ is also possible. May 28, 2022 at 22:27
  • 1
    the \. can be difficult to read sometimes, so I have changed into using [.] May 30, 2022 at 0:13
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';
};
2
  • 4
    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 such
    – GrayedFox
    Apr 26, 2018 at 13:22
  • 6
    Please never change builtin methods.
    – Vitaliy
    Mar 28, 2020 at 8:51

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.