Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

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

0 contributions
4 points

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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more