Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
How to subtract dates to calculate nights
-
- Posts: 15
- Joined: Sat Feb 01, 2020 4:04 pm
Re: How to subtract dates to calculate nights
Oh sorry, just adding that at the end. Thank you. Great, it worked.
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
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:
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:
But, I block on the calculation of the number of weeks ...
if you have an idea

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
}

if you have an idea
-
- nuBuilder Team
- Posts: 4416
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 74 times
- Been thanked: 472 times
- Contact:
Re: How to subtract dates to calculate nights
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));
}
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
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
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
-
- nuBuilder Team
- Posts: 4416
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 74 times
- Been thanked: 472 times
- Contact:
Re: How to subtract dates to calculate nights
This is not a nuBuilder-specific question, maybe you could ask/search at stackoverflow. Normally you get answers there very quickly.
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
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
I found the solution for the calculation on the dates by mixing your code and using a method of DIV and MODULO in Js