From 26039f920b38b27cf7dc0f16d43829543f704023 Mon Sep 17 00:00:00 2001 From: Sam Thomas Date: Thu, 10 Nov 2016 13:15:37 -0500 Subject: [PATCH] add validation for moments with invalid dates at construction --- src/lib/moment/constructor.js | 1 + src/test/moment/create.js | 5 +++++ 2 files changed, 6 insertions(+) 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'); +}); -- 2.47.2