Welcome to the nuBuilder Forums!
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
How to subtract dates to calculate nights
-
- Posts: 15
- Joined: Sat Feb 01, 2020 4:04 pm
How to subtract dates to calculate nights
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?
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?
-
- Posts: 15
- Joined: Sat Feb 01, 2020 4:04 pm
Re: How to subtract dates to calculate nights
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?
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?
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
Re: How to subtract dates to calculate nights
1. Add these two functions to your form's custom code field:
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.
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
}
3. Add add on onchange event (to both date fields). Replace datefield_1 and date_field_2 with your date field ids.
You do not have the required permissions to view the files attached to this post.
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
hello,
i try to use this method indicated in this post to find the number of months, but the area does not display anything:
The cell is a Text entry: I inserted the two functions in custom code Jscript:
Then I insert the onchange events in the two date fields :
Have you an idea ?
i try to use this method indicated in this post to find the number of months, but the area does not display anything:
The cell is a Text entry: 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
}
You do not have the required permissions to view the files attached to this post.
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
Re: How to subtract dates to calculate nights
new Date() doesn't accept a date with the format dd/mm/yyyy.
Use this subtractDateFields() function instead.
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));
}
Last edited by kev1n on Sun Feb 23, 2020 7:13 pm, edited 1 time in total.
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
I suspected that the dd/mm/yyyy format will be problematic.
I tried your modification but nothing is displayed ...
I tried your modification but nothing is displayed ...
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
-
- nuBuilder Team
- Posts: 4565
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 76 times
- Been thanked: 529 times
- Contact:
Re: How to subtract dates to calculate nights
I replaced split("-") with split("/") in my code. Try it againl.
-
- Posts: 67
- Joined: Wed Jul 04, 2018 4:11 pm
- Location: France
Re: How to subtract dates to calculate nights
Nothing,
my code :
.
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();
}
}