How to Check if Dates are on the Same Day in JavaScript


How can we check if two date objects are on the same day in JavaScript?

Unfortunately, this functionality is not available in the Date library, so we need to create our own utility function to handle this case.

To perform this check, we can simply check for equality between the day, month, and year.

We can use the following methods on any Date object to check if two dates are on the same day: getDate(), getMonth(), and getFullYear().

const areDatesOnSameDay = (a, b) =>
    a.getFullYear() === b.getFullYear() &&
    a.getMonth() === b.getMonth() &&
    a.getDate() === b.getDate();

And of course, we can add some types for TypeScript.

const areDatesOnSameDay = (a: Date, b: Date): boolean =>
    a.getFullYear() === b.getFullYear() &&
    a.getMonth() === b.getMonth() &&
    a.getDate() === b.getDate();