Compare equal dates in javascript

5

With reference to these two questions:

I need to compare the dates and I need to distinguish three dates between two dates, either being equal or bigger or smaller / p>

function gerarData(str) {
    var partes = str.split("/");
    return new Date(partes[2], partes[1] - 1, partes[0]);
}

function teste(){
    var data_1 = '03/03/2016';
    var data_2 = '03/03/2016';

    console.log( gerarData(data_1) );
    console.log( gerarData(data_2) );

    if ( gerarData(data_1) === gerarData(data_2) ) {
      alert("Datas iguais com ===");
    }
    else if ( gerarData(data_1) == gerarData(data_2) ) {
      alert("Datas iguais com ==");
    }
    else if ( gerarData(data_1) > gerarData(data_2) ){
      alert("data_1 > data_2");
    } 
    else if ( gerarData(data_1) < gerarData(data_2) ) {
      alert("data_1 < data_2");
    }
    else{
      alert("inconclusivo \n data_1="+gerarData(data_1)+" \n data_2="+gerarData(data_2));      
    }

}
teste();

Why even console.log showing me that they are equal does not enter the condition that makes this check?

    
asked by anonymous 02.03.2016 / 13:50

2 answers

5

In JavaScript dates are objects and two different objects are never the same.

Then new Date(1456959600000) == new Date(1456959600000) (using == or === ) always gives false .

To compare dates you can convert the date to Timestamp and in this case the comparison above would be true . In other words, it would compare numbers, the number of milliseconds of the respective date. In practice it would be:

+new Date(1456959600000) == +new Date(1456959600000)

which is basically the same as

1456959600000 == 1456959600000

An example code could be:

function gerarData(str) {
    var partes = str.split("/");
    return +new Date(partes[2], partes[1] - 1, partes[0]);
}

function teste(data_1, data_2) {
    var a = gerarData(data_1);
    var b = gerarData(data_2);

    var tipo;
    if (a < b) tipo = 'anterior';
    else if (a == b) tipo = 'igual';
    else if (a > b) tipo = 'posterior';
    else tipo = 'inconclusivo'
    return tipo;
}

jsFiddle: link

About + :

In the background, using +new Date(); or using new Date().getTime(); generates a number, timestamp. The reason why + works here is because it does a type conversion of the date object to a number. Is it called unit operator (or unary?) In English unary plus operator and what it does is convert what's after it to a numeric value. In case the date does this by invoking .valueOf() , but can also be used in Strings with numbers, or Booleans. Some examples:

+ true // 1 + false // 0 + '20' // 20 + '45deg' // NaN (in this case blending letters it does not find numeric value, parseInt would be an alternative)

Interesting reading on + (in English) :

02.03.2016 / 14:13
1

You need to use the getTime () function to convert to numbers, to get into the correct condition.

Example:

function gerarData(str) {
      var partes = str.split("/");
      return new Date(partes[2], partes[1] - 1, partes[0]).getTime();
}

function teste() {
  var data_1 = '03/03/2016';
  var data_2 = '03/03/2016';
  if (gerarData(data_1) === gerarData(data_2)) {
    alert("Datas iguais com ===");
  } else if (gerarData(data_1) == gerarData(data_2)) {
    alert("Datas iguais com ==");
  } else if (gerarData(data_1) > gerarData(data_2)) {
    alert("data_1 > data_2");
  } else if (gerarData(data_1) < gerarData(data_2)) {
    alert("data_1 < data_2");
  } else {
    alert("inconclusivo \n data_1=" + gerarData(data_1) + " \n data_2=" + gerarData(data_2));
  }

}
teste();
    
02.03.2016 / 14:02