How to check if two dates are the same day in JavaScript

How to detect whether a date object instance in JavaScript refers to the same day of another date object?

How to detect whether a date object instance in JavaScript refers to the same day of another date object?

JavaScript does not provide this feature in its standard library, but you can use the following methods to achieve it

  • getDate()Return one day
  • getMonth()Return month
  • getFullYear()Returns the 4-digit year

This is a simple function, you can copy/paste to check:

const datesAreOnSameDay = (first, second) =>
    first.getFullYear() === second.getFullYear() &&
    first.getMonth() === second.getMonth() &&
    first.getDate() === second.getDate();

Example usage:

datesAreOnSameDay(new Date(), new Date()) //true

Download mine for freeJavaScript beginner's manual


More js tutorials: