Languages
[Edit]
EN

JavaScript - compare two dates / check if dates are equal or not

4 points
Created by:
Kenya-Spears
800

In this article, we're going to have a look at how to compare two dates in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

var date1 = new Date(2020, 10, 15);
var date2 = new Date(2020, 10, 15);

console.log(date1.getTime() === date2.getTime());  // true

 

Common mistakes

Talking about date values comparison we need to look at the fact there are comparison operators that will be working and will not directly with date objects:

  • Working operators are: >, <, >= and <=,
  • Not working operators are: ==, ===, != and !===.

For not working operators it is necessary to use getTime() method and compare results, e.g. date1.getTime() === date2.getTime().

Common mistakes analysis:

Note: read this or this article to understans how == and === work for objects comparision. 

// ONLINE-RUNNER:browser;

var now = new Date();
var date1 = new Date(now);
var date2 = new Date(now);

console.log(now   ==  now  );  // true   // incorrect usage !!!
console.log(now   === now  );  // true   // incorrect usage !!!
console.log(date1 ==  date2);  // false  // incorrect usage !!!
console.log(date1 === date2);  // false  // incorrect usage !!!
console.log(date1 !=  date2);  // true   // incorrect usage !!!
console.log(date1 !== date2);  // true   // incorrect usage !!!

console.log(date1.getTime() === date2.getTime());  // true  // correct usage

Look at the below examples to see how to use comparison operators.

 

Bigger-smaller operators example

In this section presented example uses fact, that it is possible to check that dates are not in relation bigger or smaller, so they are equal.

// ONLINE-RUNNER:browser;

function compareDates(a, b) {
    if (a < b) return -1;
    if (a > b) return +1;
    return 0; / / dates are equal
}


// Usage example:

var now = new Date();
var date1 = new Date(2020, 1, 15);
var date2 = new Date(2020, 6, 20);

console.log(compareDates(now, now));      //  0  // it means dates are equal
console.log(compareDates(date1, date2));  // -1  // arg 1 is smaller (date1 < date2)
console.log(compareDates(date2, date1));  // +1  // arg 1 is bigger  (date2 > date1)

 

Equal-different operators example

The example presented in this section uses fact, if the Unix time of both dates is equal it means dates are equal.

// ONLINE-RUNNER:browser;

function compareDates(a, b) {
    if (a && b) {
        return a.getTime() === b.getTime();
    }
    return false;
}


// Usage example:

var now = new Date();
var date1 = new Date(2020, 1, 15);
var date2 = new Date(2020, 6, 20);

console.log(compareDates(now, now));      // true
console.log(compareDates(date1, date2));  // false
console.log(compareDates(date2, date1));  // false

 

See also

  1. JavaScript - universal way to compare values (boolean, number, string, Date, etc.)

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join