-1

I try to calculate the difference between two dates for example: (01/01/2020) and (31/01/2021). I looked at the momentjs module but it doesn't suit me.

I would like the result in months, weeks and days.

with my code below, I can calculate the months, the days but not the weeks.

//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 = Math.trunc(timeDiffMonth / (1000 * 60 * 60 * 24 * 365.25 / 12));
        
        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 = Math.abs(timeDiffMonth / (1000 * 60 * 60 * 24 * 365.25 / 12)).toFixed(0);
        mm = mm * 4;
       
        var timeDiffSem = Math.abs(date2.getTime() - date1.getTime());
        ss = Math.abs(timeDiffSem / (1000 * 60 * 60 * 24 * 7)).toFixed(0);
        ss = ss - mm;
       
        return ss;
    }
    
    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
    }

If you have an idea ???

joe.cocs
  • 1
  • 1
  • Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) – Abhinav Suman Feb 27 '20 at 11:53
  • thanks for the link, I was inspired by an example that I modified and integrated into my project. [Code][/Code] – joe.cocs Feb 28 '20 at 14:02

3 Answers3

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


weeksBetween(new Date(), new Date(2014, 6, 22));

This will give you weeks between two dates

Arsalan Afridi
  • 209
  • 3
  • 13
  • Thank's but it's not my answer. – joe.cocs Feb 27 '20 at 12:07
  • @joe.cocs you already wrote that with your code you found number of month and days so with this one you can find weeks also. Other wise i would suggest moment.js – Arsalan Afridi Feb 27 '20 at 12:12
  • If I ask my question here it is that precisely despite the fact that I found the months and the days, I cannot determine how to find the number of weeks and especially when I test that it works. the week part does not correspond systematically. I do not wish to use moment.js, I would like to understand how it works to find the number of weeks and that it is fair each time. – joe.cocs Feb 27 '20 at 12:58
0

This might help you. You could get the difference in months and add that to the initial date; then get the difference in weeks and add that to the initial date again.

Sandbox: https://codesandbox.io/s/compassionate-bas-fg1c2

var moment = require("moment");

var a = moment("2021-01-31");
var b = moment("2020-01-01");

var months = a.diff(b, "months");
b.add(months, "months");

var weeks = a.diff(b, "week");
b.add(weeks, "weeks");

var days = a.diff(b, "days");

console.log(months + " months " + weeks + " weeks " + days + " days");

joy08
  • 9,004
  • 8
  • 38
  • 73
  • I do not wish to use moment.js, I would like to understand how it works to find the number of weeks and that it is fair each time. – joe.cocs Feb 27 '20 at 12:59
0

I was inspired by an example that I modified and integrated into my project.

For those interested, I wanted to recover the values ​​of two 'date' fields formatted in 'dd / mm / yyyy' and calculate the difference. I wanted a result in months, weeks and days.

//Fonction pour calculer le Nb de jours entre deux dates f1 et f2
    function timeDifferenceMonths(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]);
//variables pour définir les unités d'un jour, semaine, mois et année
        var oneDay = 60 * 60 * 24; // secondes*minutes*heures
        var oneWeek = 7; // secondes*minutes*heures*7jours/semaine
        var oneMonth = 365.25 / 12; // secondes*minutes*heures*jours / 12mois
        var oneYear = 365.25; // secondes*minutes*heures*jours en 1 an
//calcul de la différences entre les dates en jours        
        var days = Math.round((date2 - date1) / (60 * 60 * 24 * 1000));
        
//calcul toutes les années et récuperer le reste
/*        var differenceYears = Math.trunc(days / oneYear);
        days = Math.trunc(days % oneYear);
*/
//calcul tous les mois restants et récuperer le reste
        var differenceMonths = Math.trunc(days / oneMonth);
        days = Math.abs(days % oneMonth);

//calcul toutes les semaines restantes et récuperer le reste
        var differenceWeeks = Math.abs(days / oneWeek);
        days = Math.abs(days % oneWeek);

//le reste de jours
        var differenceDays = Math.abs(days);

        return differenceMonths;

    }

    function dureeprojetsMonth(f1, f2) {
      $('#Duree_ProjetsMois').val(timeDifferenceMonths(f1,f2)).change();
    }

I commented on the passage to calculate the years if you want a result with the extra years

joe.cocs
  • 1
  • 1