Are you looking to determine if two date object instances in JavaScript refer to the same day? JavaScript doesn’t come with built-in functionality for this, but you can easily implement it using a few methods.
The getDate()
method returns the day, the getMonth()
method returns the month, and the getFullYear()
method returns the four-digit year. By comparing these values between two date objects, you can check if they represent the same day.
Here’s a simple function that you can copy and paste to perform this check:
const datesAreOnSameDay = (first, second) =>
first.getFullYear() === second.getFullYear() &&
first.getMonth() === second.getMonth() &&
first.getDate() === second.getDate();
To use this function, call it with two date objects as arguments. It will return true
if they are on the same day, and false
otherwise. Here’s an example usage:
datesAreOnSameDay(new Date(), new Date()); // true
With this function, you can easily compare two date objects in JavaScript and determine if they represent the same day.