Moment.js is now considered a legacy project (see Moment.js Project Status).
- You don't (may not) need Moment.js
- 4 alternatives to Moment.js for internationalizing dates
- You Probably Don't Need Moment.js Anymore - DockYard
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"
// Native
new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZone: 'UTC',
timeZoneName: 'short',
}).format(date);
// '08/19/2024, 10:09:24 UTC'