Skip to content

Commit

Permalink
Merge pull request #3506 from samuelfullerthomas:develop
Browse files Browse the repository at this point in the history
[bugfix] Fix invalid moments returning valid dates to method calls
  • Loading branch information
ichernev committed Nov 12, 2016
2 parents 104cf6e + 4ba3f90 commit fefc7c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib/moment/constructor.js
Expand Up @@ -60,6 +60,9 @@ 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) {
Expand Down
6 changes: 6 additions & 0 deletions src/test/moment/create.js
Expand Up @@ -1100,3 +1100,9 @@ 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.ok(momentAsDate instanceof Date, 'toDate returns a Date object');
assert.ok(isNaN(momentAsDate.getTime()), 'toDate returns an invalid Date invalid');
});

0 comments on commit fefc7c8

Please sign in to comment.