Validate if entered date is smaller than the current javaScript [duplicate]

1

Personal Beauty,

I'm trying to validate if an informed date is less than the current date ..

var dtVenc = '22/10/2018'; /* Aqui recebe a data String do Json*/

function retornaData(data){
	if(!data){
		return data;	
	}
	split = data.split('/');
	return new Date( split[1] + "/" +split[0]+"/"+split[2] );
}

var dataCurrente = new Date();

	if(retornaData(dtVenc).getTime() < dataCurrente.getTime()){

    	alert("A data informada é inferir a data atual");

    }

I am validating if the reported date is less than the current date. So far so good! It is validating correctly, the problem is when I inform the same day of the current date, there it falls in the validation.

And it's wrong, because the reported date (equal to current) is not less.

I do not understand much this date problem with JavaScript

    
asked by anonymous 22.10.2018 / 20:54

2 answers

2

follows the answer:

 var dtVenc = '23/10/2018'; /* Aqui recebe a data String do Json*/

    function retornaData(data) {
        if (!data) {
            return data;
        }
        split = data.split('/');
        return new Date(split[1] + "/" + split[0] + "/" + split[2]);
    }

    var dataCurrente = new Date();

    if (retornaData(dtVenc).getDate() < dataCurrente.getDate()) {
        console.log("A data informada é inferir a data atual");
    }
    
22.10.2018 / 21:01
1
  

Running the code below you will understand why when you inform the same day of the current date it falls into validation.

var dtVenc = '22/10/2018'; /* Aqui recebe a data String do Json*/

function retornaData(data){
	if(!data){
		return data;	
	}
	split = data.split('/');
	return new Date( split[1] + "/" +split[0]+"/"+split[2] );
}

var dataCurrente = new Date();

	if(retornaData(dtVenc).getTime() < dataCurrente.getTime()){

    console.log("A data informada é inferir a data atual");

  }
  
  console.log(retornaData(dtVenc).getTime()); //1540177200000
  
  console.log(dataCurrente.getTime());
    
    var jsTimestamp = new Date(1540177200000);
    
    console.log(jsTimestamp);

When you tell the date only with day, month and year% with%, JavaScript assumes the date to be '22/10/2018' , that is, zero time of day reported, so the expression "2018-10-22T03:00:00.000Z" will always return% with%. In turn, today's date of expression retornaData(dtVenc).getTime() will always be increasing, and obviously always greater than the value 1540177200000 , every second you run the above code.

  

In order for it to work correctly you should put a time on the expiration date dataCurrente.getTime() , see:

var dtVenc = '22/10/2018'; /* Aqui recebe a data String do Json*/

function retornaData(data){
	if(!data){
		return data;	
	}
	split = data.split('/');
	return new Date( split[1] + "/" +split[0]+"/"+split[2] + " 23:59:59");
}

var dataCurrente = new Date();

	if(retornaData(dtVenc).getTime() < dataCurrente.getTime()){

    console.log("A data informada é inferir a data atual");

  }else{

       console.log("A data ainda não venceu");

  }

console.log(retornaData(dtVenc).getTime());
console.log(dataCurrente.getTime());
    
22.10.2018 / 22:10