Last Updated On By Anmol Lohana
JavaScript has a built-in system to compare dates, which makes it easy for date comparison. There are many methods to compare two dates in JavaScript. We will compare dates using various methods listed below with examples.
Table of Contents
Comparison Operators are less than (<), greater than (>), less than and equal to (<=), greater than and equal to (>=), equal to (==), strictly equal to (===), not equal to (!=), strictly not equal to (!==). These comparison operators can be used to compare two dates in JavaScript, which we will be doing below.
var date1 = new Date('2021-04-01');
var date2 = new Date('2021-03-31');
var g = date1 > date2;
var g_e = date1 >= date2;
var l = date1 < date2;
var l_e = date1 <= date2;
var g = date1 == date2;
var g_e = date1 === date2;
var l = date1 != date2;
var l_e = date1 !== date2;
console.log("Date1 is greater than Date2 ");
console.log("date1 > date2 : " , g);
console.log("Date1 is greater than and equal to Date2 ");
console.log("date1 >= date2 : " , g_e);
console.log("Date1 is less than Date2 ");
console.log("date1 < date2 : " , l);
console.log("Date1 is less than and equal to Date2 ");
console.log("date1 <= date2 : " , l_e);
console.log("Date1 is equal to Date2 ");
console.log("date1 == date2 : " , g);
console.log("Date1 is strictly equal to Date2 ");
console.log("date1 === date2 : " , g_e);
console.log("Date1 is not equal to Date2 ");
console.log("date1 != date2 : " , l);
console.log("Date1 is strictly not equal to Date2 ");
console.log("date1 !== date2 : " , l_e);
We can convert dates in time numeric format using getTime() Method. And then compare those numeric values to compare dates.
let date_1 = new Date(2019, 08, 07, 11, 45, 55);
let date_2 = new Date(2019, 08, 03, 11, 45, 55);
let d1 = date_1.getTime();
let d2 = date_2.getTime();
if (d1 !== d2)
console.log("Dates are not equal");
if (d1 === d2)
console.log("Dates are equal");
We can apply other operations as well that we did in the first example. Here, we see an example, so we only used two of them to check out. And we will follow the same process for rest of the examples.
The valueOf() Method works in similar way as getTime() Method works. It also converts the date in a numeric value. And then we can compare those numeric values to compare dates.
let date_1 = new Date(2019, 08, 07, 11, 45, 55);
let date_2 = new Date(2019, 08, 03, 11, 45, 55);
let d1 = date_1.valueOf();
let d2 = date_2.valueOf();
if (d1 < d2)
console.log("Date1 is less than date2");
if (d1 > d2)
console.log("Date1 is greater than date2");
The Number() function converts date into a number which represents its numeric value in JavaScript. If not converted into a legal number, it will return NaN (Not a Number).
let date_1 = new Date(2019, 08, 07, 11, 45, 55);
let date_2 = new Date(2019, 08, 03, 11, 45, 55);
let d1 = Number(date_1);
let d2 = Number(date_2);
if (d1 != d2)
console.log("Number1 is not equal to Number2");
if (d1 == d2)
console.log("Number1 is equal to Number2");
The unary operator takes an argument to operate or operand. JavaScript has many other unary operators, but we will use plus + operator to convert date into a number.
let date_1 = new Date(2019, 08, 07, 11, 45, 55);
let date_2 = new Date(2019, 08, 07, 11, 45, 55);
let d1 = +date_1;
let d2 = +date_2;
console.log("+date1 === +date2", d1===d2);
console.log("+date1 !== +date2", d1!==d2);
In this article, we discussed comparison of two dates in JavaScript. We saw five different ways of JavaScript date comparison. By using comparison operators, some methods, and unary operator. We performed some coding examples as well to understand the concepts more clearly.