The following function provides an easy way to format Javascript dates in ISO-8601 format without using
date.toISOString()
, which could lead to timezone issues as the date is first converted to UTC which
could lead to discrepancies in the result in certain timezones.
/**
* Formats the provided date in _ISO-8601 format_ without using
* `toISOString()` to avoid timezone issues
*
* @param date The Date object
* @returns {string}
*/
function formatDate(date) {
return [
`${date.getFullYear()}`,
`${date.getMonth() + 1}`.padStart(2, '0'),
`${date.getDate()}`.padStart(2, '0')
].join('-')
}