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 ???