From: Maggie Pint Date: Mon, 9 Jan 2017 04:49:27 +0000 (-0800) Subject: toISOString returns null for invalid date X-Git-Tag: 2.18.0~34^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8c81bd9091b55e3c917a74f9a7419e496071370f;p=thirdparty%2Fmoment.git toISOString returns null for invalid date --- diff --git a/src/lib/moment/format.js b/src/lib/moment/format.js index 539f6c8e1..f6bef4151 100644 --- a/src/lib/moment/format.js +++ b/src/lib/moment/format.js @@ -9,18 +9,19 @@ export function toString () { return this.clone().locale('en').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'); } -export function toISOString () { +export function toISOString() { + if (!this.isValid()) { + return null; + } var m = this.clone().utc(); - if (0 < m.year() && m.year() <= 9999) { - if (isFunction(Date.prototype.toISOString)) { - // native implementation is ~50x faster, use it when we can - return this.toDate().toISOString(); - } else { - return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); - } - } else { + if (m.year() < 0 || m.year() > 9999) { return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); } + if (isFunction(Date.prototype.toISOString)) { + // native implementation is ~50x faster, use it when we can + return this.toDate().toISOString(); + } + return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'); } /** diff --git a/src/test/moment/format.js b/src/test/moment/format.js index 34ca0e719..10fe7cf0c 100644 --- a/src/test/moment/format.js +++ b/src/test/moment/format.js @@ -148,6 +148,10 @@ test('toISOString', function (assert) { // big negative years date = moment.utc('-020123-10-09T20:30:40.678'); assert.equal(date.toISOString(), '-020123-10-09T20:30:40.678Z', 'ISO8601 format on big negative year'); + + //invalid dates + date = moment.utc('2017-12-32'); + assert.equal(date.toISOString(), null, 'An invalid date to iso string is null'); }); // See https://nodejs.org/dist/latest/docs/api/util.html#util_custom_inspect_function_on_objects