Validate if informed date is less than the current Avascript

Asked

Viewed 641 times

1

Personal Beauty,

I am 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’m validating if the date is less than the current date. So far, so good! It is validating correctly, the problem is when I report the same day of the current date, ai falls into validation.

And it is wrong, because the informed date (equal to the current one) is no less.

I don’t understand, much this date problem with Javascript

2 answers

2


following 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");
    }

1

Running the code below you will understand why when you report the same day the current date 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 give the date only with day, month and year '22/10/2018' Javascript assumes the date to be "2018-10-22T03:00:00.000Z", that is, zero hour of the day reported, hence the expression retornaData(dtVenc).getTime() will always return the value 1540177200000. In turn, today’s date of the expression dataCurrente.getTime() will always be growing, and obviously always greater than the value 1540177200000, every second you run the above code.

For it to work properly you should put a time on the due date return new Date( split[1] + "/" +split[0]+"/"+split[2] + " 23:59:59");, 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());

Browser other questions tagged

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