how to calculate the amount of days with date input?

Asked

Viewed 741 times

-1

My question is this::

I have two input type date.

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

How am I gonna do that?

  • Difference between dates, [How to calculate the days difference using "date" input? duplicate]

  • 6-2=4, not 5

  • Put the code you already have, so it’s easier.

  • Welcome Marco Aurélio, if any answer solved your problem be sure to mark it as accepted, see https://i.stack.Imgur.com/evLUR.png and why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply/1079#1079

  • 1

    was lack of coffee guy kkkkkkkkkk, thank you.

4 answers

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 computer’s internal clock and returns a date object for that instant.

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

  • Or document.getElementById("numeroDias").value = isNaN(difDias()) ? "Selecione a outra data" : difDias();

  • 1

    @dvd, ball show, the Selecione a outra data

  • Thanks! It worked out right here.

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');
}

  • 1

    now I see, my Chara kkkkkkkk

  • 1

    I don’t know if you’ve got the answer to your question, but there it is

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)));

0

Try to catch the difference between the two dates. Being a the initial date and b the final 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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.