From: Sam Thomas Date: Thu, 10 Nov 2016 18:15:37 +0000 (-0500) Subject: add validation for moments with invalid dates at construction X-Git-Tag: 2.17.0~7^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26039f920b38b27cf7dc0f16d43829543f704023;p=thirdparty%2Fmoment.git add validation for moments with invalid dates at construction --- diff --git a/src/lib/moment/constructor.js b/src/lib/moment/constructor.js index 964c0aef6..a42301420 100644 --- a/src/lib/moment/constructor.js +++ b/src/lib/moment/constructor.js @@ -60,6 +60,7 @@ var updateInProgress = false; export function Moment(config) { copyConfig(this, config); this._d = new Date(config._d != null ? config._d.getTime() : NaN); + if (!this.isValid()) this._d = new Date(NaN); // Prevent infinite loop in case updateOffset creates new moment // objects. if (updateInProgress === false) { diff --git a/src/test/moment/create.js b/src/test/moment/create.js index 549b3c941..53b29789c 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -1100,3 +1100,8 @@ test('parsing only meridiem results in invalid date', function (assert) { assert.ok(moment('02:30 p more extra stuff', 'hh:mm a').isValid(), 'because other tokens were parsed, date is valid'); assert.ok(moment('1/1/2016 extra data', ['a', 'M/D/YYYY']).isValid(), 'took second format, does not pick up on meridiem parsed from first format (good copy)'); }); + +test('invalid dates return invalid for methods that access the _d prop', function (assert) { + var momentAsDate = moment(['2015', '12', '1']).toDate() + assert.equal(momentAsDate, 'Invalid Date', 'toDate returns invalid'); +});