how to calculate the number of days with input of type date? [duplicate]

0

My question is:

I have two input of type date.

What I want to do in Javascript is to calculate the amount of days from the first to the second, ie if the person put dateEnter = day 06/02/2018 in the first input and dataSaida = 06/06/2018 in the second , then it is calculated how many days have passed in a new input, that is, 4 days.

How will I do this?

    
asked by anonymous 02.06.2018 / 06:47

4 answers

1

function calculaDiferenca(dataInicial, dataFinal) {

    /*gera um objeto do tipo Date com valor do input*/
    var date1 = new Date(dataInicial);        
    var date2 = new Date(dataFinal);

    console.log(date2.getTime());
    /*Subtrai a segunda data em milisegundos pela primeira e usa função abs para retornar o valor absoluto*/
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());

    /*agora ele divide o valor da diferença das datas em milisegundos pela quantidade de milisegundos em um dia e usa ceil para 
    retorna o menor número inteiro*/
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

    alert(diffDays + ' dias');
}
    
03.06.2018 / 20:09
3

/******* função que recupera os valores dos inputs tipo date e
calcula a diferença entre eles. *******/

function difDias(){
    //instantaneos do objeto Date, veja explicação no final da resposta
    var dataUm = new Date(document.getElementById("dataUm").value);
    var dataDois = new Date(document.getElementById("dataDois").value);
    return parseInt((dataUm - dataDois) / (24 * 3600 * 1000));
}

function chamar(){
    document.getElementById("numeroDias").value = isNaN(difDias()) ? "Selecione a outra data" : difDias();  
}
<input type="date" class="textbox" id="dataDois" onchange="chamar()">

<input type="date" class="textbox" id="dataUm" onchange="chamar()"/>

<input type="text" class="textbox" id="numeroDias" name="numdays"/>
  

The Date object takes a snapshot of the internal clock of the computer and returns a date object for that instant.

     Internally, the value of a date object instance is the hour, in milliseconds, from zero hour of January 1, 1970, in the Greenwich Mean Time timezone - the world standard reference point for all time conversions.

    
02.06.2018 / 08:19
0
// Creditos: https://stackoverflow.com/questions/2627473/how-to-calculate-the-number-of-days-between-two-dates

var umDia = 24*60*60*1000; // horas*minutos*segundos*milisegundos

var dataEntrada = new Date(2018,06,02);

var dataSaida = new Date(2018,06,06);

var difDias = Math.round(Math.abs((dataEntrada.getTime() - 
dataSaida.getTime())/(umDia)));
    
02.06.2018 / 07:10
0

Try to get the difference between the two dates. Being a the start date and b the end date:

// créditos
// https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript

var _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
  // Discard the time and time-zone information.
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

// test it
var a = new Date("2017-01-01"),
    b = new Date("2017-07-25"),
    difference = dateDiffInDays(a, b);
    
document.write(difference + " dias de diferença");
    
02.06.2018 / 07:12