Skip to content

Instantly share code, notes, and snippets.

Last active August 16, 2023 12:21
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Moment.js deprecated, date-fns issues

Moment.js is deprecated and date-fns issues


Moment.js is now considered a legacy project (see Moment.js Project Status).
Here's a comparison between the alternatives:

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
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);
// "2020-01-01T00:00:00.000Z"


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment