How to tell if two dates are in the same day or in the same hour? [duplicate]

The Date prototype has APIs that allow you to check the year, month, and day-of-month, which seems simple and effective.

You’ll want to decide whether your application needs the dates to be the same from the point of view of the locale where your code runs, or if the comparison should be based on UTC values.

function sameDay(d1, d2) {
  return d1.getFullYear() === d2.getFullYear() &&
    d1.getMonth() === d2.getMonth() &&
    d1.getDate() === d2.getDate();
}

There are corresponding UTC getters getUTCFullYear(), getUTCMonth(), and getUTCDate().

Leave a Comment