Moment.js is deprecated and date-fns
issues
Synopsis
Moment.js is now considered a legacy project (see Moment.js Project Status).
Here's a comparison between the alternatives:
- https://github.com/you-dont-need/You-Dont-Need-Momentjs
- https://blog.logrocket.com/4-alternatives-to-moment-js-for-internationalizing-dates/
Issues with date-fns
new Date(2020, 0, 1).toISOString();
// "2019-12-31T22:00:00.000Z"
new Date(Date.UTC(2020, 0, 1)).toISOString();
// "2020-01-01T00:00:00.000Z"
// Passing a string has quirks:
// 'YYYY-MM-DD' (ISO date-only) is treated as UTC
new Date('2020-01-01').toISOString();
// "2020-01-01T00:00:00.000Z"
// 'YYYY-MM-DDTHH:mm:ss' (date-time) is treated as local time
new Date('2020-01-01T00:00:00').toISOString();
// "2019-12-31T22:00:00.000Z"
// Adding a time-zone will use that time-zone
new Date('2020-01-01T00:00:00Z').toISOString();
// "2020-01-01T00:00:00.000Z"
new Date('2020-01-01T00:00:00+00:00').toISOString();
// "2020-01-01T00:00:00.000Z"
// Get date values in UTC
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_components_and_time_zones
new Date('2020-01-01T00:00:00Z').getUTCHours();
// 0
new Date('2020-01-01T00:00:00Z').getUTCDate();
// 1
// We can adjust a date object in UTC:
const dateObj = new Date();
dateObj.setUTCFullYear(2020, 0, 1);
dateObj.setUTCHours(0, 0, 0, 0);
dateObj.toISOString();
// "2020-01-01T00:00:00.000Z"