From 6abd6fde48f7136067f77c70d1c4d64195733f23 Mon Sep 17 00:00:00 2001 From: Ash Searle Date: Fri, 18 Jan 2019 10:16:26 +0000 Subject: [PATCH] Address review comments --- src/lib/create/date-from-array.js | 17 +++++++++++------ src/lib/moment/start-end-of.js | 6 ++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/lib/create/date-from-array.js b/src/lib/create/date-from-array.js index 1d577207fe..7fabbc05f1 100644 --- a/src/lib/create/date-from-array.js +++ b/src/lib/create/date-from-array.js @@ -1,12 +1,16 @@ export function createDate (y, m, d, h, M, s, ms) { - // the date constructor remaps years 0-99 to 1900-1999 - var remapYears = (y < 100 && y >= 0); // can't just apply() to create a date: // https://stackoverflow.com/q/181348 - var date = new Date(remapYears ? y + 400 : y, m, d, h, M, s, ms); - - if (remapYears && isFinite(date.getFullYear())) { - date.setFullYear(y); + var date; + // the date constructor remaps years 0-99 to 1900-1999 + if (y < 100 && y >= 0) { + // preserve leap years using a full 400 year cycle, then reset + date = new Date(y + 400, m, d, h, M, s, ms); + if (isFinite(date.getFullYear())) { + date.setFullYear(y); + } + } else { + date = new Date(y, m, d, h, M, s, ms); } return date; @@ -17,6 +21,7 @@ export function createUTCDate (y) { // the Date.UTC function remaps years 0-99 to 1900-1999 if (y < 100 && y >= 0) { var args = Array.prototype.slice.call(arguments); + // preserve leap years using a full 400 year cycle, then reset args[0] = y + 400; date = new Date(Date.UTC.apply(null, args)); if (isFinite(date.getUTCFullYear())) { diff --git a/src/lib/moment/start-end-of.js b/src/lib/moment/start-end-of.js index 75942674e4..42c19cbc3b 100644 --- a/src/lib/moment/start-end-of.js +++ b/src/lib/moment/start-end-of.js @@ -13,7 +13,8 @@ function mod(dividend, divisor) { function localStartOfDate(y, m, d) { // the date constructor remaps years 0-99 to 1900-1999 - if (0 <= y && y <= 99) { + if (y < 100 && y >= 0) { + // preserve leap years using a full 400 year cycle, then reset return new Date(y + 400, m, d) - MS_PER_400_YEARS; } else { return new Date(y, m, d).valueOf(); @@ -22,7 +23,8 @@ function localStartOfDate(y, m, d) { function utcStartOfDate(y, m, d) { // Date.UTC remaps years 0-99 to 1900-1999 - if (0 <= y && y <= 99) { + if (y < 100 && y >= 0) { + // preserve leap years using a full 400 year cycle, then reset return Date.UTC(y + 400, m, d) - MS_PER_400_YEARS; } else { return Date.UTC(y, m, d);