How to Check if Two Dates are the Same Day in JavaScript
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:
1 | const datesAreOnSameDay = (first, second) => |
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:
1 | 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.
tags: [“JavaScript”, “date comparison”, “same day”, “getDate”, “getMonth”, “getFullYear”]