reza reza - 2 months ago 17
TypeScript Question

how to tell if two dates are in the same day?

I am using the moment npm module.
I am comparing two dates and want to see if there in the same day.

Is there a clean way of doing that using the moment package
or using straight javascript or typescript?

Answer

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().