Languages
[Edit]
EN

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

4 points
Created by:
Marcin
15710

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

 

Talking about date values comparision we need to look at fact there are comprision 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 as below examples to see how to use comparision operators:

1. 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)

2. Equal-different operators example

In this section presented example uses fact, if 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

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

â€ïžđŸ’» 🙂

Join