Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

How to subtract dates to calculate nights

Questions related to using nuBuilder Forte.
kev1n
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

Unread post by kev1n »

Add these 2 alert() functions to your code and tell me what you see in the message boxes.

Code: Select all

   function subtractDateFields(f1, f2) {
 
        alert('f1: ' + f1 + ' / ' + ' f2: ' + 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());
		
	alert('date1:' + date1+ ' / date2: ' + date2 + ' timeDiff: ' + timeDiff);
		
        return Math.round(timeDiff / (2e3 * 3600 * 365.25));
    }
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 happens,

it's as if the onchange function is not happening.
is it because it's a nuDate object?

I changed to text entry but it doesn't work either
kev1n
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

Unread post by kev1n »

It works here. Add a simple alert('hello'). Does this show up?
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 »

Yes alert works
kev1n
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

Unread post by kev1n »

It looks like dureeprojets() is not getting called / found.
Add these two functions to the main form's custom code field, not a subform.

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));
    }
   
    function dureeprojets(f1, f2) {
        $('#Duree_Projets').val(subtractDateFields(f1,f2)); // <----- replace with your object id
    }
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 just tried by putting the code in the main form and removing that from the iFrame.

nothing.

I tried to put the code with the alerts, nothing.

my form consists of a main form and an iFrame with a form in it, and a third iFrame with another Form in the second:
Date calcul.png
in the third I will also have a date interval calculation in days ;)
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 »

When in doubt I tried with another browser: firefox, safari and edge but nothing either
kev1n
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

Unread post by kev1n »

Are there no errors in the developer console? (F12)
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 »

Yes i have an error
Capture d’écran 2020-02-24 à 09.53.02.png
You do not have the required permissions to view the files attached to this post.
kev1n
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

Unread post by kev1n »

nuCalChoice() raises an error for some unknown reason. I would set a breakpoint in that function and execute step by step to figure out where the error occurs.

If you are not familiar with the developer console, read this:
https://developers.google.com/web/tools ... javascript
Post Reply