]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
add validation for moments with invalid dates at construction
authorSam Thomas <samuel.thomas@qubit.com>
Thu, 10 Nov 2016 18:15:37 +0000 (13:15 -0500)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 12 Nov 2016 06:12:45 +0000 (22:12 -0800)
src/lib/moment/constructor.js
src/test/moment/create.js

index 964c0aef6a67d96a756cd7d24433ba331cc527ab..a42301420559487e68fa657f7a201c3a44e97c12 100644 (file)
@@ -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) {
index 549b3c941ddaa0f6ee64a2d42083998be73203f9..53b29789cb2309be7f0f6cdb8711c35c746c6a95 100644 (file)
@@ -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');
+});