Counter of days elapsed since a certain date, using the form [closed]

-7

I would like someone to help me with this script.

As in the image, I need to get the date I'm going to enter in the blanks and count how many days have passed up to the day.

Could anyone help me with this?

    
asked by anonymous 29.08.2017 / 18:38

2 answers

0

I think you misunderstood the purpose of the site.

In any case, your problem is quite simple and can be solved with a few lines of code.

I did not do everything for you because, as already said, this is not the purpose of the site, but here is a basic idea of how it can be done.

Subtracting a date from another date returns a numeric value, this value corresponds to the number of milliseconds elapsed between the first date and the second date. So, having this amount in hand, you just need to use some good old basic math to get the amount of days elapsed between these two dates.

const txtData = document.getElementById('data');
const txtDias = document.getElementById('dias');
const btComparar = document.getElementById('comparar');
btComparar.addEventListener('click', buttonClick);

function buttonClick(){
  var dtTarget = new Date(txtData.value);

  var diff = diferencaEmDias(new Date(), dtTarget);
  console.log(diff);
  
  txtDias.value = diff;
}

function diferencaEmDias(dataPrincipal, dataSecundaria) {
  return Math.round((dataPrincipal - dataSecundaria)/(1000*60*60*24)) - 1;
}
Data: <input type="date" id="data" />
Dias: <input type="text" id="dias" />

<button id="comparar"> Comparar </button>
    
29.08.2017 / 18:56
0

It seems like the date input format is MM / DD / YYYY, and the end date would be the current date. Therefore the current date must be formatted for MM / DD / YYYY so that you can make the proper calculations.

When you need calculations between dates, 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 given.

That's the only 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>
    
29.08.2017 / 22:10