Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ashsearle committed Jan 18, 2019
1 parent e2c82e1 commit 6abd6fd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 11 additions & 6 deletions 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;
Expand All @@ -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())) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/moment/start-end-of.js
Expand Up @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit 6abd6fd

Please sign in to comment.