How to compare dates using JavaScript
In JavaScript, Date is a built-in object that represents a specific point in time. It provides methods and properties to work with dates and times.
This Answer provides a comparison between two dates in JavaScript, along with practical examples to aid in understanding their implementation.
One approach to compare two dates in JavaScript involves converting them into numeric values that represent their respective time. Using the getTime() function, we can transform the Date object into a numeric value. Once both dates are converted into numeric values, direct comparison becomes possible.
Note: The
getTime()method enables us to perform various forms of date comparisons using comparison operators such as >, <, <=, >=, ==, ===, !=, and !==.
Comparing dates with equality operators
To perform equality comparisons in JavaScript, utilize the Date object along with the getTime() method, which returns the number of milliseconds. For more specific details such as the day, month, and more, employ additional date methods like getDate(), getHours(), getDay(), getMonth(), and getYear().
Code example 1
// Create two date objectslet currentDate = new Date();let anotherDate = new Date();// Compare the time of the two datesif (currentDate.getTime() === anotherDate.getTime()) {console.log("Both dates are equal");} else {console.log("Dates are not equal");}
Explanation
Here’s the explanation of the code above.
Lines 1–2: We create two
Dateobjects, i.e,currentDate, andanotherDate, using thenew Date()constructor, which initializes them with the current date and time.Lines 4–5: We check if the values of
date1anddate2are equal using the===operator. We also usegetTime(), which returns the numeric value representing the number of milliseconds. If the numeric values are equal, the code executes the block inside theifstatement, which logs the message“Both dates are equal”to the console.Lines 6–8: If the numeric values are not equal, the code executes the block inside the
elsestatement, which logs the message“Dates are not equal”to the console.
We can make a comparison by providing various dates as inputs to the date object.
Code example 2
const date1 = new Date("2023-05-26");const date2 = new Date("2022-06-19");if (date1.getTime() !== date2.getTime()) {console.log("Dates are not equal");} else {console.log("Both dates are equal");}
Explanation
Here’s the explanation of the code above.
Lines 1–2: We declare
date1anddate2using thenew Date()constructor, which initializes them with the current date and time. We also take the arguments of different dates, respectively.Lines 4–5: If the numeric values are not equal, the code executes the block inside the
ifstatement, which logs the message“Dates are not equal”to the console.Lines 6–8: If the numeric values are equal, the code executes the block inside the
elsestatement, which logs the message“Both dates are equal”to the console.
Comparing dates with relational operators
To perform a specific date comparison, we can utilize the getFullYear() method of the date object in the following manner when comparing specific values like the year.
Code example 3
const date1 = new Date("05/26/2023");const date2 = new Date("07/28/2021");const year1 = date1.getFullYear();const year2 = date2.getFullYear();if (year1 < year2) {console.log("Year1 is lesser than Year2");} else if (year1 > year2) {console.log("Year1 is greater than Year2");} else {console.log("Both years are equal");}
Explanation
Here’s an explanation of the above code .
Lines 1–2: We declare two
Dateobjects:date1with the date “2023-05-26”, anddate2with the date “2021-07-28”.Lines 4–5: We use the
getFullYear()method to extract the full-year value fromdate1anddate2, and store them inyear1andyear2, respectively.Lines 7–13: We use an
ifstatement to compareyear1andyear2:If
year1is less thanyear2, the code block inside theifstatement is executed, and the message“Year1 is earlier than Year2”is logged to the console.If
year1is greater thanyear2, the code block inside theelse ifstatement is executed, and the message“Year1 is later than Year2”is logged to the console.If neither of the conditions above is true, the code block inside the
elsestatement is executed, and the message“Both years are equal”is logged.
Free Resources