-7
It seems to me that the date input format is MM/DD/YYYY, and the final date would be the current date. Therefore the current date must be formatted for MM/DD/YYYY so that proper calculations can be made.
When calculations between dates are required, remember the javascript lingua franca: milliseconds.
In order to make calculations between dates, we use getTime()
which returns the number of milliseconds between midnight on January 1, 1970 and the date provided.
Then you just make a difference.
function calc() {
//pega o valor (data) digitada no campo (input) de id=dataInicial
var inicial = document.getElementById("dataInicial").value;
var DataDesde = new Date(inicial);
var date = new Date();
//data atual formatada para MM/DD/AAAA
DataHoje = new Date(((date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear()));
var difTempo = Math.abs(DataHoje.getTime() - DataDesde.getTime());
var difDias = Math.ceil(difTempo / (1000 * 3600 * 24));
document.getElementById('dias').value = difDias;
document.getElementById("resultado").className="show";
}
function esc() {
document.getElementById("resultado").className="hide";
}
.hide{
display:none;
}
.show{
display:block;
}
<form action="javascript:void(0);">
Entre com o formato da data MM/DD/AAAA<br>
Data: <input type="text" id="dataInicial"><br>
<div id="resultado" class="hide">
Transcorridos <input type="text" id="dias" size="3"> dias.<br>
</div>
<input type="reset" value="Limpar" onclick="esc()"> <input type="submit" onclick="calc()" value="Calcular">
</form>
Do you simply want someone to do it for you? Or are you struggling with something?
– Jéf Bueno
I am really in need of help, in my course, we have not yet come to see const, we still use var. I spent the afternoon reading on forums and other sites, but are very evolved for what we are still seeing in cur, but anyway, thanks.
– Bruno Mota
You’re welcome. Indication of reading: var, const or Let? Which to use?
– Jéf Bueno
say, the format has to be month/day/year?
– user60252