How to Compare Dates in Javascript
Posted on: February 14, 2021 by Ariessa Norramli
In this article, you will learn how to Compare Dates in Javascript.
Let’s say you have two dates: 14 / 2 / 2020
and 14 / 2 / 2021
.
var date1 = new Date(2021, 02, 14);
var date2 = new Date(2020, 02, 14);
In order to compare dates, you can use the getTime()
method and a strict equality comparison ===
.
var date1 = new Date(2021, 02, 14);
var date2 = new Date(2020, 02, 14);
if (date1.getTime() === date2.getTime())
console.log("The dates are equal");
else
console.log("The dates are not equal");
Note: The getTime
method functions by returning the number of milliseconds between the midnight of 1/1/1970 and the supplied date.
Share on social media
//