As you've discovered, moment.js compares the local date (which is probably the most common requirement). To compare as UTC, you can either:
- Convert to a timestamp in timezone +0 and compare just the date part, or
- Divide the date's time value by the number of milliseconds in one day and floor it (since in ECMAScript UTC days always have exactly 8.64e7 ms)
Option 1
If you start with Date objects, you can just do:
date1.toLocaleDateString('en-ca',{timeZone:'UTC'}) == date2.toLocaleDateString('en-ca',{timeZone:'UTC'})
as language "en-ca" generates a date in format YYYY-MM-DD (you could probably use any language, but an ISO 8601 like format seems appropriate). The important part is timezone: 'UTC'
Another option is the date part of toISOString:
date1.toISOString().substring(0,10) == date2.toISOString().substring(0,10)
If you really want to use moment.js, use it to format the dates as UTC timestamps:
/** Return true if date1 and date2 are same UTC day
*
* @param {Date} date1
* @param {Date} date2
* @returns {boolean} true if dates match, false otherwise
*/
function isSameUTCDay(date1, date2) {
let format = 'YYYY-MM-DD';
let d1 = moment(date1).utc();
let d2 = moment(date2).utc();
return d1.format(format) == d2.format(format);
}
// Same UTC day but different local day anywhere but UTC +0
let date1 = "2000-01-01T00:00:00Z";
let date2 = "2000-01-01T23:59:59Z";
console.log(isSameUTCDay(date1, date2));
<script src="https://unpkg.com/moment@2.27.0/moment.js"></script>
Option 2
To compare as time values:
/** Return true if the UTC date for date1 and date2 are the same
*
* @param {Date} date1
* @param {Date} date2
* @returns {boolean} true if dates match, false otherwise
*/
function isSameUTCDay(date1, date2) {
return Math.floor(+date1 / 8.64e7) == Math.floor(+date2 / 8.64e7);
}
// Same UTC day but different local day anywhere but UTC +0
let date1 = "2000-01-01T00:00:00Z";
let date2 = "2000-01-01T23:59:59Z";
// Should be true everywhere
console.log(isSameUTCDay(new Date(date1), new Date(date2)));
PS. Using the built–in parser is strongly discouraged, however since the timestamp format is supported by ECMAScript I think it's OK for this example.