]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix #2645 - invalid dates pre-1970
authorDavid Reiman <david.reiman@creditkarma.com>
Fri, 2 Oct 2015 17:15:04 +0000 (10:15 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 9 Nov 2015 05:17:51 +0000 (21:17 -0800)
src/lib/create/date-from-array.js
src/test/moment/create.js

index 349eebcc4f1b5614a8ec1a7aa113e34a746319ce..6c8053c5ec69fde6219bd6e194378a805fa7a390 100644 (file)
@@ -4,7 +4,7 @@ export function createDate (y, m, d, h, M, s, ms) {
     var date = new Date(y, m, d, h, M, s, ms);
 
     //the date constructor doesn't accept years < 1970
-    if (y < 1970) {
+    if (y < 1970 && isFinite(date.getYear()) ) {
         date.setFullYear(y);
     }
     return date;
@@ -12,7 +12,7 @@ export function createDate (y, m, d, h, M, s, ms) {
 
 export function createUTCDate (y) {
     var date = new Date(Date.UTC.apply(null, arguments));
-    if (y < 1970) {
+    if (y < 1970 && isFinite(date.getUTCFullYear())) {
         date.setUTCFullYear(y);
     }
     return date;
index e58ca2660d94e9adab340db2e94c1ec804094085..42ec14dff707b1e9076490745510d6478fc856f4 100644 (file)
@@ -14,6 +14,11 @@ test('array', function (assert) {
     assert.equal(+moment(new Date(2010, 1, 14, 15, 25, 50, 125)), +moment([2010, 1, 14, 15, 25, 50, 125]), 'constructing with array === constructing with new Date()');
 });
 
+test('array with invalid arguments', function (assert) {
+    assert.ok(!moment([2010, null, null]).isValid(), '[2010, null, null]');
+    assert.ok(!moment([1945, null, null]).isValid(), '[1945, null, null] (pre-1970)');
+});
+
 test('array copying', function (assert) {
     var importantArray = [2009, 11];
     moment(importantArray);