Calculate time between two dates: hours, minutes and seconds

2

I am calculating a range of days between two dates with hours, minutes and seconds as follows:

function calcularDiasAusencia(fechaIni, fechaFin)
{
var fecha1 = new   Date(fechaIni.substring(0,4),fechaIni.substring(5,7),fechaIni.substring(8,10));
var fecha2 = new Date(fechaFin.substring(0,4),fechaFin.substring(5,7),fechaFin.substring(8,10));
var diasDif = fecha2.getTime() - fecha1.getTime();
var dias = Math.round(diasDif/(1000 * 60 * 60 * 24));

document.getElementById('diasAusentismo').value =  dias+1;

}

Where In date is 2016-10-31 20:14:31 and End date is 2016-11-03 20:14:35, counting from October 31 until November 3 there should be 4 days but the system shows only 3 in the same way I get the bad calculation doing it in the following way

What can generate the error so that the function is throwing incorrect values?

    
asked by Santiago Muñoz 24.11.2016 в 02:31
source

3 answers

2

What happens is that the months in the date function go from 0 to 11 and you are looking at the wrong dates. 2016-10-31 is December 1 and 2016-11-03 is December 3. The solution would be to subtract one from the past months as parameters to the Date function.

You can see everything running on this jsfiddle link

jsfiddle link

    
answered by 24.11.2016 / 11:56
source
2

The result of the subtraction of both dates is correct. The error is that the day of departure ( Fecha Inicio ) is not being contemplated , so you would be missing add one more day.

  

For example : from 2016-09-30 to 2016-10-01 , there is exactly 1 day. But in your case it should be 2 , since 2016-09-30 will also be absent.

Try doing the calculation like this:

function calcularDiasAusencia(fechaIni, fechaFin) {
  var diaEnMils = 1000 * 60 * 60 * 24,
      desde = new Date(fechaIni.substr(0, 10)),
      hasta = new Date(fechaFin.substr(0, 10)),
      diff = hasta.getTime() - desde.getTime() + diaEnMils;// +1 incluir el dia de ini
  return diff / diaEnMils;
}

document.getElementById('calcular').addEventListener('click', function() {
  document.getElementById('dias').innerText = calcularDiasAusencia(
    document.getElementById('fechaIni').value,
    document.getElementById('fechaFin').value
  );
});
Desde: <input id="fechaIni" placeholder="yyy-mm-dd HH:MM:SS" value="2016-09-30 07:30:00" /><br>
Hasta: <input id="fechaFin" placeholder="yyy-mm-dd HH:MM:SS" value="2016-10-03 07:30:00" /><br>
<button type="button" id="calcular">Calcular</button><br>
Total días ausente: <span id="dias"></span>
    
answered by 24.11.2016 в 13:53
1

For situations involving calculations with dates and times I always recommend moment.js , eg:

var fecha1 = moment("2016-09-30 07:30:00", "YYYY-MM-DD HH:mm:ss");
var fecha2 = moment("2016-10-03 07:30:00", "YYYY-MM-DD HH:mm:ss");

var diff = fecha2.diff(fecha1, 'd'); // Diff in days
console.log(diff);

var diff = fecha2.diff(fecha1, 'h'); // Diff in hours
console.log(diff);

See a full example .

Note that between 2016-09-30 and 2016-09-30 there are ZERO days, not a day. It is possible that what you want to obtain is not the time between two dates, but the size of the interval defined by two dates. That's as simple as adding 1 to the result (or round upwards if the interval is not full days).

    
answered by 24.11.2016 в 13:45