2
The code below for date comparison works for if condition >=
and Else.
function validaDatas(){
var dataInicial = new Date($("input[name='datainicial']").val());
var dataFinal = new Date($("input[name='datafinal']").val());
if (!dataInicial || !dataFinal) return false;
if (dataInicial >= dataFinal) {
alert("dataInicial maior que DataFinal ou datas iguais");
}else{
alert("dataInicial menor que DataFinal");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="javascript:void(0)" onsubmit="return validaDatas()">
<div>
<label>Data inicial:</label>
<input type="date" name="datainicial" />
<label>Data Final:</label>
<input type="date" name="datafinal" />
</div>
<button>Enviar</button>
</form>
But if you put a else if
separating the condition of equal dates ==
the script does not work in this condition
function validaDatas(){
var dataInicial = new Date($("input[name='datainicial']").val());
var dataFinal = new Date($("input[name='datafinal']").val());
if (!dataInicial || !dataFinal) return false;
if (dataInicial > dataFinal) {
alert("dataInicial maior que DataFinal");
}else if (dataInicial == dataFinal){
alert("dataInicial igual a DataFinal");
}else{
alert("dataInicial menor que DataFinal");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="javascript:void(0)" onsubmit="return validaDatas()">
<div>
<label>Data inicial:</label>
<input type="date" name="datainicial" />
<label>Data Final:</label>
<input type="date" name="datafinal" />
</div>
<button>Enviar</button>
</form>
BECAUSE?
Diego Marques, very good!! Only one thing, if you put Alert(stardate) and Alert(stardateFinal) will see that although it says that it is Brazil’s official schedule with GMT -3 the date is the previous day. You know why?
– user60252
I edited the reply with those remarks :)
– Diego Marques
Thanks Diego Marques! Better than that only two of it :)
– user60252