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.

How to subtract dates to calculate nights

Questions related to using nuBuilder Forte.
faroinsua
Posts: 15
Joined: Sat Feb 01, 2020 4:04 pm

How to subtract dates to calculate nights

Unread post 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?
kev1n
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

Unread post by kev1n »

faroinsua
Posts: 15
Joined: Sat Feb 01, 2020 4:04 pm

Re: How to subtract dates to calculate nights

Unread post 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?
kev1n
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

Unread post 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
You do not have the required permissions to view the files attached to this post.
joecocs
Posts: 67
Joined: Wed Jul 04, 2018 4:11 pm
Location: France

Re: How to subtract dates to calculate nights

Unread post 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 ?
You do not have the required permissions to view the files attached to this post.
kev1n
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

Unread post 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));
}
Last edited by kev1n on Sun Feb 23, 2020 7:13 pm, edited 1 time in total.
joecocs
Posts: 67
Joined: Wed Jul 04, 2018 4:11 pm
Location: France

Re: How to subtract dates to calculate nights

Unread post by joecocs »

I suspected that the dd/mm/yyyy format will be problematic.

I tried your modification but nothing is displayed ...
kev1n
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

Unread post by kev1n »

Can you post the code you're using?
kev1n
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

Unread post by kev1n »

I replaced split("-") with split("/") in my code. Try it againl.
joecocs
Posts: 67
Joined: Wed Jul 04, 2018 4:11 pm
Location: France

Re: How to subtract dates to calculate nights

Unread post 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();

        }
}
.
Post Reply