Difference of javascript days considering dates in different months

1

I need to get the difference between two dates. However, in my business rule, the difference of 05/17/2014 and 05/17/2014 is 1 day.
So I thought just adding a day to the end result would solve my problem. However, when I ask for the range of (for example) 9/30/2014 to 10/1/2014 it returns me the expected result. 2 days. If I ask from 30 to 30, it returns me 0.

Current script:

function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[1], mdy[0]-1);
}

function daydiff(first, second) {
    var date1 = parseDate(first);
    var date2 = parseDate(second);
    return (date2-date1)/(1000*60*60*24) + 1;
}

Why does this happen and how do I adjust this comparison of differences for my case?

    
asked by anonymous 26.01.2015 / 16:57

2 answers

1

I built this function based on searches. I think it's better than using a library, because then you go straight to the point you do not carry extra resources, however it is a restrictive one, you would have to adapt.

function betweenDates(d1, d2, diff, returnLiteral){

    d1      = d1.split(' ');    // Divide o timestamp em data e hora
    d1[0]   = d1[0].split('-'); // Separa as variacoes da data
    d1[1]   = d1[1].split(':'); // Separa as variacoes da hora
    d1      = d1[0].concat(d1[1]); // concatena os dois conteudos formando um array unico.

    d1 = new Date(d1[0],d1[1],d1[2],d1[3],d1[4],d1[5]); // gera o objeto date
    d1 = Date.UTC(d1.getFullYear(), d1.getMonth(), d1.getDate(), d1.getHours(), d1.getMinutes(), d1.getSeconds()); // retona o time UTC corespondente da data.

    d2      = d2.split(' ');
    d2[0]   = d2[0].split('-');
    d2[1]   = d2[1].split(':');
    d2      = d2[0].concat(d2[1]);

    d2 = new Date(d2[0],d2[1],d2[2],d2[3],d2[4],d2[5]);
    d2 = Date.UTC(d2.getFullYear(), d2.getMonth(), d2.getDate(), d2.getHours(), d2.getMinutes(), d2.getSeconds());

    var dDiff = d2 - d1; // calcula a diferenca entre as datas

    var out = {
        'y' : dDiff/1000/60/60/24/30/12,    // calculo para ano
        'm' : dDiff/1000/60/60/24/30,       // calculo para mes
        'd' : dDiff/1000/60/60/24,          // calculo para dia
        'h' : dDiff/1000/60/60,             // calculo para hora
        'i' : dDiff/1000/60,                // calculo para minuto
        's' : dDiff/1000/1                  // calculo para segundo
    };

    out = Math.floor(out[diff]);    // Saida (inteiro do calculo)

    // Retorno
    if(out < 0 && !returnLiteral){
        return out*-1;
    }else{
        return out;
    }
}

betweenDates('2015-11-02 00:00:00', '2015-10-02 23:00:00', 'y') // 0
betweenDates('2015-11-02 00:00:00', '2015-10-02 23:00:00', 'm') // -1
betweenDates('2015-10-02 00:00:00', '2015-11-02 23:00:00', 'm') // 1
betweenDates('2015-10-02 00:00:00', '2015-10-02 23:00:00', 's') // 82800
betweenDates('2015-10-02 00:00:00', '2015-10-02 23:00:00', 'h') // 23
    
03.11.2015 / 13:31
1

The problem with your code is the month, which is indexed to zero, but you are subtracting from the day. Also, the result of the division may return an unexpected number, so it is necessary to round with Math.ceil() .

function parseDate(str) { // aceita dia/mes/ano
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[1] - 1, mdy[0]);
}

function daydiff(first, second) {
    var date1 = parseDate(first);
    var date2 = parseDate(second);
    return Math.ceil((date2 - date1) / (1000 * 60 * 60 * 24)) + 1;
}

// exibição dos resultados
var pre = document.body.appendChild(document.createElement('pre'));
pre.textContent += '30/09/2014 a 30/09/2014: ';
pre.textContent += daydiff('30/09/2014', '30/09/2014') + ' dia(s)\n';
pre.textContent += '30/09/2014 a 01/10/2014: ';
pre.textContent += daydiff('30/09/2014', '01/10/2014') + ' dia(s)\n';
pre.textContent += '30/09/2014 a 30/10/2014: ';
pre.textContent += daydiff('30/09/2014', '30/10/2014') + ' dia(s)\n';
    
03.11.2015 / 13:50