Page 1 of 5
How to subtract dates to calculate nights
Posted: Sat Feb 15, 2020 7:47 pm
by faroinsua
The date fields are not included as objects in the calc field object list. I first included the header code as indicated in
https://forums.nubuilder.cloud/viewtopic. ... der#p19824
to be able to edit the calc field but I need to use date fields which are not listed as objects so I can not use them.
'date_field1'-'date_field2' wont work. May I use sql code like Datediff?
Re: How to subtract dates to calculate nights
Posted: Sun Feb 16, 2020 6:29 am
by kev1n
Re: How to subtract dates to calculate nights
Posted: Sun Feb 16, 2020 11:13 am
by faroinsua
Thank you. So I have other 2 questions.
I would use
var date1 = new Date("10/18/2016");
var date2 = new Date("10/21/2016");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var numberOfNights = Math.ceil(timeDiff / (1000 * 3600 * 24));
then. But instead of dates I need field names, so var date1 = new Date('res_ent'); if res_ent is the start date field for example?
And do I use the calc field in nuBuilder or the "Custom Code" field? Which Event do I select to to trigger the javascript?
Re: How to subtract dates to calculate nights
Posted: Sun Feb 16, 2020 12:28 pm
by kev1n
1. Add these two functions to your form's custom code field:
Code: Select all
function subtractDateFields(f1, f2) {
var date1 = new Date($('#'+f1).val());
var date2 = new Date($('#'+f2).val());
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
return Math.ceil(timeDiff / (1000 * 3600 * 24));
}
function numberOfNights(f1, f2) {
$('#nights').val(subtractDateFields(f1,f2)); // <----- replace with your object id
}
2. Then, to display the number of nights, use an object (in this example with Id: "nights") of type input (input type text).
3. Add add on onchange event (to both date fields). Replace datefield_1 and date_field_2 with your date field ids.
cust_code_onchange.png
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 4:16 pm
by joecocs
hello,
i try to use this method indicated in this post to find the number of months,
Capture d’écran 2020-02-23 à 15.50.12.png
but the area does not display anything:
The cell is a Text entry:
Capture d’écran 2020-02-23 à 16.07.55.png
I inserted the two functions in custom code Jscript:
Code: Select all
function subtractDateFields(f1, f2) {
var date1 = new Date($('#'+f1).val());
var date2 = new Date($('#'+f2).val());
var monthDiff = Math.abs(date2.getMonth() - date1.getMonth());
return Math.ceil(monthDiff);
}
function dureeprojets(f1, f2) {
$('#Duree_projets').val(subtractDateFields(f1,f2)); // <----- replace with your object id
}
Then I insert the onchange events in the two date fields :
Capture d’écran 2020-02-23 à 16.06.23.png
Capture d’écran 2020-02-23 à 16.06.50.png
Have you an idea ?
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 4:59 pm
by kev1n
new Date() doesn't accept a date with the format dd/mm/yyyy.
Use this subtractDateFields() function instead.
Code: Select all
function subtractDateFields(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 timeDiff = Math.abs(date2.getTime() - date1.getTime());
return Math.round(timeDiff / (2e3 * 3600 * 365.25));
}
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 6:24 pm
by joecocs
I suspected that the dd/mm/yyyy format will be problematic.
I tried your modification but nothing is displayed ...
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 6:58 pm
by kev1n
Can you post the code you're using?
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 7:13 pm
by kev1n
I replaced split("-") with split("/") in my code. Try it againl.
Re: How to subtract dates to calculate nights
Posted: Sun Feb 23, 2020 9:04 pm
by joecocs
Nothing,
my code :
Code: Select all
if(!nuMainForm() && nuFormType() == 'edit') {
nuHide('Ref_Clients');
function subtractDateFields(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 timeDiff = Math.abs(date2.getTime() - date1.getTime());
return Math.round(timeDiff / (2e3 * 3600 * 365.25));
}
function dureeprojets(f1, f2) {
$('#Duree_Projets').val(subtractDateFields(f1,f2)); // <----- replace with your object id
}
if(nuIsNewRecord()) {
$('#Ref_Clients_Projets').val(parent.nuFORM.getCurrent().record_id).change();
}
}
.