Compare equal dates in javascript

Asked

Viewed 1,620 times

5

With reference to these two questions:

I need to compare the dates and I need three distinctions between two dates, she being equal or greater or minor See the example below:

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 the console.log showing me that they are equal he does not enter the condition that makes this check?

  • I think you could just add one + here: return new Date, getting return +new Date.

  • 2

    @Sergio why? Give an answer.

  • @mustache made :)

2 answers

5


In Javascript dates are objects and two different objects are never equal.

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

To compare dates can convert the date to Timestamp and in that 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 of 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: https://jsfiddle.net/grggk9xb/

About the +:

In the background use +new Date(); or use 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. It is called unit operator (or unary?), in English unary plus operator and what it does is convert what is after it to a numerical value. In the case of the date it does this by invoking the .valueOf(), but can also be used in Strings with numbers, or boolean. Some examples:

+true // 1 +false // 0 +'20' // 20 +'45deg' // Nan (in this case mixing letters it does not find numerical value, parseint would be an alternative)

Interesting reading on the + (in English):

  • Sergio, could you give a reference or explanation of why to use the + ? What is your function in this placement before the new in the code ?

  • 2

    @devgaspa yes, I will. The + is a shortcut of . getTime(), I will add that to the answer soon I had to leave for meeting now.

  • 1

    @Sergio my biggest doubt is because the + solve this.

  • 1

    @bigown take a look, today I found time to explain better.

1

You need to use the getTime() function to convert to numbers, so that you enter 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();

  • 1

    Gabriel, the builder Date Javascript with string needs a date in the format mm/dd/aaaaa. I believe that’s why he used the split. Other than that, the getTime will solve his problem.

  • If you use this way, the constructor will understand that you are passing a string mm/dd/aaaa and the date will be reversed, for example new Date('04/03/2016') the object date in the object will be 03 de Abril de 2016 and not 04 de Março de 2016 that would be the expected date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.