Skip to content

Commit

Permalink
Ensuring that Date#toString returns "Invalid Date" when the date's va…
Browse files Browse the repository at this point in the history
…lue is NaN.
  • Loading branch information
ljharb committed Feb 18, 2015
1 parent a0e0544 commit 208080c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,19 @@
});
}

if (String(new Date(NaN)) !== 'Invalid Date') {
var dateToString = Date.prototype.toString;
var shimmedDateToString = function toString() {
var valueOf = +this;
if (valueOf !== valueOf) {
return 'Invalid Date';
}
return dateToString.call(this);
};
defineProperty(shimmedDateToString, 'toString', dateToString.toString, true);
defineProperty(Date.prototype, 'toString', shimmedDateToString, true);
}

// Annex B HTML methods
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-additional-properties-of-the-string.prototype-object
var stringHTMLshims = {
Expand Down
7 changes: 7 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*global describe, it, expect, require */

describe('Date', function () {
it('when invalid, dates should toString to "Invalid Date"', function () {
expect(String(new Date(NaN))).to.equal('Invalid Date');
});
});

0 comments on commit 208080c

Please sign in to comment.