Check If Two Dates Are The Same Day In JavaScript

A simple function to check if two date objects are referring to the same day.


JavaScript date objects

Unfortunately, this is not a base functionality of the date object. Instead, we can use the getDate, getMonth and getFullYear methods to check if the dates are the same.

const sameDay = (firstDate, secondDate) =>
    firstDate.getFullYear() === secondDate.getFullYear() &&
    firstDate.getMonth() === secondDate.getMonth() &&
    firstDate.getDate() === secondDate.getDate();

The usage looks like this

const isSameDay = sameDay(new Date(), new Date()); // true

#