Difference between dates

Asked

Viewed 12,698 times

7

Every time I pass the difference in dates in the same month, it works, but when it’s between different months, a negative value comes. How do I make a difference between dates? See my code. The error is in total

Code

if (($("#txtDateStart").val().split("/") == "") && ($("#txtDateEnd").val().split("/") == "")) {
    data1 = new Date();
    data2 = new Date();
}
else {
    var dtInicio = $("#txtDateStart").val().split("/");
    var dtFim = $("#txtDateEnd").val().split("/");
    data1 = new Date(dtInicio[2] + "/" + dtInicio[1] + "/" + dtInicio[0]);
    data2 = new Date(dtFim[2] + "/" + dtFim[1] + "/" + dtFim[0]);
}
var total = data2.getDate() - data1.getDate();
  • 2

    I took this code and it worked. var total = (((Date.parse(data2)) - (Date.parse(data1))) / (24 * 60 * 60 * 1000));

  • 1

    To 'finalize' the question, post your answer and/or provide feedback on why the answers from colleagues have not worked for you.

  • You haven’t got the answer yet?

  • Why haven’t you chosen an answer yet?

3 answers

18

Based on one of the best OS replies in English and applied to your specific case:

if ( ($("#txtDateStart").val().split("/") == "") &&
     ($("#txtDateEnd").val().split("/") == "") ) {
   data1 = new Date();
   data2 = new Date();
}
else {
   var dtInicio = $("#txtDateStart").val().split("/");
   var dtFim = $("#txtDateEnd").val().split("/");
   data1 = new Date(dtInicio[2] + "/" + dtInicio[1] + "/" + dtInicio[0]);
   data2 = new Date(dtFim[2] + "/" + dtFim[1] + "/" + dtFim[0]);
}
var total = dateDiferencaEmDias( data1, data2 );

// a e b são objetos Date do JS
function dateDiferencaEmDias(a, b) {
   // Descartando timezone e horário de verão
   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) / ( 1000 * 60 * 60 * 24) );
}

7

Example:

date1 = new Date('2014/01/01');
date2 = new Date('2014/04/01');
var diferenca = Math.abs(date1 - date2); //diferença em milésimos e positivo
var dia = 1000*60*60*24; // milésimos de segundo correspondente a um dia
var total = Math.round(diferenca/dia); //valor total de dias arredondado 

0

var total = (((Date.parse(data2)) - (Date.parse(data1))) / (24 * 60 * 60 * 1000));
  • 4

    What’s the difference between your solution and your colleagues' solution: that one and that one? Which led you to post a new response instead of branding the possible response that helped you out as correct?

  • @Fernando, I don’t think you read the posts. Just got a reply from me now and commented. Read everything, time and etc and then post your comment. See what led me to reply now. It’s there somewhere.

  • 2

    @Fernando one difference, for example, is that this one does not take into account peculiarities of Timezone. I imagine that on a day that you have a change of daylight time, for example, the result may have an undue variation.

  • @pnet, I read yes! After all not post and I do not comment something without reading! And I only questioned the difference between your solution and the others, because I considered the others even more complete and comprehensive than yours, because as Bacco commented they treat Timezone, negative days, not yours. So I’d like to know the problems of the others that led you not to choose them as an answer and to have the need to post another answer that doesn’t present anything relatively new in relation to the others because the formula of multiplying the difference by the days, minutes, seconds and milliseconds are already present in the other answers?

  • @Fernando, I haven’t set up an answer yet, I don’t know why you’re like this. I can make another one, but not mine, I don’t see a problem with that. As the solution at the time was in a comment and I no longer opened this page, nor activate the answers that were given later. I received the communication by Sopt Anderson Madeira who was questioning that I had commented on the solution, but I had not finished and as I always try to finish, although there are some open questions still, I went and answered, but I did not finish.

Browser other questions tagged

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