Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
What to do now that Moment.js is considered a legacy project?
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' (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"
// 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment