Page 5 of 5

Re: How to subtract dates to calculate nights

Posted: Tue Feb 25, 2020 7:46 pm
by faroinsua
Oh sorry, just adding that at the end. Thank you. Great, it worked.

Re: How to subtract dates to calculate nights

Posted: Wed Feb 26, 2020 6:47 pm
by joecocs
I come back with my date formulas ... :)

in the forms I manage to take a period between my 2 dates and calculate the number of weeks and days with the following formula:

Code: Select all

//Mettre à jour le champs durée en Nb de Semaine
    function subtractDateFields1(f1, f2) {
   
        var f1 = $('#'+f1).val().split("/");
        var date1 = new Date(f1[2], f1[1] - 1, f1[0]);

        var f2 = $('#'+f2).val().split("/");
        var date2 = new Date(f2[2], f2[1] - 1, f2[0]);
        
        var timeDiffSem = Math.abs(date2.getTime() - date1.getTime());
        s = Math.trunc((timeDiffSem / (1000 * 60 * 60 * 24 * 7)));
        
        return s;

    }
    
    function dureetranchestvxSem(f1, f2) {
        $('#Duree_TranchesTvxSem').val(subtractDateFields1(f1,f2)).change(); // <----- replace with your object id
    }

    
//Mettre à jour le champs durée en Nb de Jour
    function subtractDateFields2(f1, f2) {
   
        var f1 = $('#'+f1).val().split("/");
        var date1 = new Date(f1[2], f1[1] - 1, f1[0]);

        var f2 = $('#'+f2).val().split("/");
        var date2 = new Date(f2[2], f2[1] - 1, f2[0]);


        var timeDiffSem = Math.abs(date2.getTime() - date1.getTime());
        s = Math.trunc((timeDiffSem / (1000 * 60 * 60 * 24 * 7)));
        
        var timeDiffJour = Math.abs(date2.getTime() - date1.getTime());
        j = Math.round((timeDiffJour / (1000 * 60 * 60 * 24 )));
        j = j - (s * 7);
        
        return Math.round(j);

    }
    
    function dureetranchestvxJour(f1, f2) {
        $('#Duree_TranchesTvxJour').val(subtractDateFields2(f1,f2)).change(); // <----- replace with your object id
    }

On the other hand,

in another form I would like to calculate the number of months, the number of weeks and the number of days for the period between these two dates.

I tried with this code:

Code: Select all

    //Mettre à jour le champs durée en Nb de mois
    function subtractDateFields1(f1, f2) {

        var f1 = $('#'+f1).val().split("/");
        var date1 = new Date(f1[2], f1[1] - 1, f1[0]);

        var f2 = $('#'+f2).val().split("/");
        var date2 = new Date(f2[2], f2[1] - 1, f2[0]);
        
        var timeDiffMonth = Math.abs(date2.getTime() - date1.getTime());
        m = (timeDiffMonth / (1000 * 60 * 60 * 24 * 365.25 / 12)).toFixed(2);
        
        return m;
        
        }

    function dureeprojetsMonth(f1, f2) {
        $('#Duree_ProjetsMois').val(subtractDateFields1(f1,f2)).change(); // <----- replace with your object id
    }
    
    //Mettre à jour le champs durée en Nb de Semaine
    function subtractDateFields2(f1, f2) {
   
        var f1 = $('#'+f1).val().split("/");
        var date1 = new Date(f1[2], f1[1] - 1, f1[0]);

        var f2 = $('#'+f2).val().split("/");
        var date2 = new Date(f2[2], f2[1] - 1, f2[0]);
        
        var timeDiffMonth = Math.abs(date2.getTime() - date1.getTime());
        mm = (timeDiffMonth / (1000 * 60 * 60 * 24 * 365.25 / 12)).toFixed(2);
        
        var timeDiffSem = Math.abs(date2.getTime() - date1.getTime());
        ss = (timeDiffSem / (1000 * 60 * 60 * 24 * 7)).toFixed(2);
        ss = ss - (mm * 4.33);
        
        return (ss).toFixed(2);

    }
    
    function dureeprojetsSem(f1, f2) {
        $('#Duree_ProjetsSem').val(subtractDateFields2(f1,f2)).change(); // <----- replace with your object id
    }

    
//Mettre à jour le champs durée en Nb de Jour
    function subtractDateFields3(f1, f2) {
   
        var f1 = $('#'+f1).val().split("/");
        var date1 = new Date(f1[2], f1[1] - 1, f1[0]);

        var f2 = $('#'+f2).val().split("/");
        var date2 = new Date(f2[2], f2[1] - 1, f2[0]);


        var timeDiffSem = Math.abs(date2.getTime() - date1.getTime());
        s = Math.trunc((timeDiffSem / (1000 * 60 * 60 * 24 * 7)));
        
        var timeDiffJour = Math.abs(date2.getTime() - date1.getTime());
        j = Math.round((timeDiffJour / (1000 * 60 * 60 * 24 )));
        j = j - (s * 7);
        
        return Math.round(j);

    }
    
    function dureeprojetsJour(f1, f2) {
        $('#Duree_ProjetsJour').val(subtractDateFields3(f1,f2)).change(); // <----- replace with your object id
    }
But, I block on the calculation of the number of weeks ... :(

if you have an idea

Re: How to subtract dates to calculate nights

Posted: Wed Feb 26, 2020 6:51 pm
by kev1n
You can try this function to calculate the number of weeks between two days:

Code: Select all

function weeksBetween(d1, d2) {
    return Math.round((d2 - d1) / (7 * 24 * 60 * 60 * 1000));
}

Re: How to subtract dates to calculate nights

Posted: Wed Feb 26, 2020 6:57 pm
by joecocs
thanks,
that's already what i do,

but for a given period i want the number of months then on the rest the number of weeks then on the rest the number of days.

that's what worries me

Re: How to subtract dates to calculate nights

Posted: Wed Feb 26, 2020 7:07 pm
by kev1n
This is not a nuBuilder-specific question, maybe you could ask/search at stackoverflow. Normally you get answers there very quickly.

Re: How to subtract dates to calculate nights

Posted: Sat Feb 29, 2020 10:38 am
by joecocs
Thanks for the advice,

I found the solution for the calculation on the dates by mixing your code and using a method of DIV and MODULO in Js