Skip to content

Instantly share code, notes, and snippets.

@ProdigySim
Last active June 26, 2020 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProdigySim/9efefdeb387ba8b14c88c163a1f462f0 to your computer and use it in GitHub Desktop.
Save ProdigySim/9efefdeb387ba8b14c88c163a1f462f0 to your computer and use it in GitHub Desktop.
Add 1 Day is timezone sensitive in moment
function addDayTest(day, tz) {
const start = moment.tz(day, 'YYYY-MM-DD', tz);
const plusDay = start.clone().add(1, 'day'); // Next calendar day at same time.
const plusHours = start.clone().add(24, 'hours'); // 24 hours in the future.
return {
start: start.valueOf() / 1000,
plusDay: plusDay.valueOf() / 1000,
plusHours: plusHours.valueOf() / 1000,
diff: (plusHours - plusDay) / 1000, // How different are "1 day" and "24 hours"?
};
}
addDayTest('2020-03-07', 'America/Chicago')
// Object { start: 1583560800, plusDay: 1583647200, plusHours: 1583647200, diff: 0 }
// Daylight savings time "spring forward" date in USA
addDayTest('2020-03-08', 'America/Chicago')
// Object { start: 1583647200, plusDay: 1583730000, plusHours: 1583733600, diff: 3600 }
// UTC observes no DST
addDayTest('2020-03-08', 'UTC')
// Object { start: 1583625600, plusDay: 1583712000, plusHours: 1583712000, diff: 0 }
// Phoenix (and most of Arizona) observe no DST
addDayTest('2020-03-08', 'America/Phoenix')
// Object { start: 1583650800, plusDay: 1583737200, plusHours: 1583737200, diff: 0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment