9
I need a simple comparison between javascript dates the end date cannot be less than the start date. I found some scripts on the internet and tried to adapt to mine, but since I know practically nothing, it didn’t work.
Follow the script I’m adapting.
<script language="javascript">
function checarDatas(){
var form_insere = document.form_insere;
var data_1 = form_insere.datainicial.value;
var data_2 = form_insere.datafinal.value;
var Compara01 = parseInt(data_1.split("/")[2].toString() + data_1.split("/")[1].toString() + data_1.split("/")[0].toString());
var Compara02 = parseInt(data_2.split("/")[2].toString() + data_2.split("/")[1].toString() + data_2.split("/")[0].toString());
if (Compara01 > Compara02) {
alert("Data não pode ser maior que a data final");
return false;
}
else {
return true
}
}
</script>
also put onsubmit="return checarDatas()"
on the form tag.
The goal is when the user enters an initial date (07/05/2014) and then on the final date (06/04/2014) triggers an alert indicating that the dates are wrong and do not let him submit the form until back and correct.
Thank you, and I accept any kind of suggestions.
"You can not accept an answer in 7 minutes" It worked yes... With much less code yet :) Idea: Is there any way he can get a reliable date from somewhere so he can also compare and not put the dates in the past tense? For example: 05/05/2014 yesterday.
– Vinicius
I imagine there’s a way he could get a date from the user’s watch for this kind of comparison, but there’s a way around it if the user changes the time of their site.
– Vinicius
@Vinicius, yes the user-side dates are dependent on the browser and can be changed by the user. Best to use server-side time.
– Sergio
because if you put a } Else if (data_1 == data_2) { does not work and always falls on Else
– user60252
@Leocaracciolo two date objects always give different. Ie
new Date() == new Date()
always gives false.– Sergio
and how will I check for equal dates?
– user60252
if I put >= in the same works
– user60252
@LeoCaracciolo https://answall.com/a/67850/129
– Sergio
but there is no input type date
– user60252
@Leocaracciolo input type date always gives a string. Asks a question that gives a help.
– Sergio