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 !==.
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()
.
// 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");}
Here’s the explanation of the code above.
Lines 1–2: We create two Date
objects, i.e, currentDate
, and anotherDate
, using the new Date()
constructor, which initializes them with the current date and time.
Lines 4–5: We check if the values of date1
and date2
are equal using the ===
operator. We also use getTime()
, which returns the numeric value representing the number of milliseconds. If the numeric values are equal, the code executes the block inside the if
statement, 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 else
statement, 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.
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");}
Here’s the explanation of the code above.
Lines 1–2: We declare date1
and date2
using the new 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 if
statement, 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 else
statement, which logs the message “Both dates are equal”
to the console.
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.
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");}
Here’s an explanation of the above code .
Lines 1–2: We declare two Date
objects: date1
with the date “2023-05-26”, and date2
with the date “2021-07-28”.
Lines 4–5: We use the getFullYear()
method to extract the full-year value from date1
and date2
, and store them in year1
and year2
, respectively.
Lines 7–13: We use an if
statement to compare year1
and year2
:
If year1
is less than year2
, the code block inside the if
statement is executed, and the message “Year1 is earlier than Year2”
is logged to the console.
If year1
is greater than year2
, the code block inside the else if
statement 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 else
statement is executed, and the message “Both years are equal”
is logged.
Free Resources