Welcome to the nuBuilder Forums!

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

JS Adding Days to Dates?

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

JS Adding Days to Dates?

Unread post by pmjd »

Hello,

I've looked at the code library on how to add days to dates but can't figure it out.
https://github.com/nuBuilder/nuBuilder- ... t_add_days
I've had more success with the php way of adding date to records after the form is entered and the record is saved. However I now need to do this on the form using JS before commiting the record.

Why?
I have a form for solution preparation. It can pull through various bits of reference information fro the solution being made via a lookup, and copy that information into the form (so that for future the reference information used to make the solution is there, and if the recipe changes in the future it won't alter the old records).

On my form for solution preparation I have a *prepared_on* date field. Once populated it should trigger and populate the *calc_expiry* date field, which is achieved by adding the *prepared_on* field and a number in days from another field *shelf_life*. The *shelf_life * field infomation is pulled through via a lookup to the substance being prepared and copied into the prep form.
If the calculated expiry is correct, then a button press by the user will copy that information to an "expires_on* field. If the calculated expiry is incorrect (such as a solution ingredient having a short shelf life) then the corret date can be added manually.

I've run nuHash and it looks like the fields are avaiable as hash cookies.

Is there a way to get this workflow to work in nuBuilder? or are the date calculation differences between sql/php and js going to cause issues?The previosu code I had worked in php but not sure how to translate that to JS to use the oninput events to trigger the changes.

Thanks,
Paul
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: JS Adding Days to Dates?

Unread post by kev1n »

calc_expiry (date) = prepared_on (date) + shelf_life (number of days):

Code: Select all

var oPreparedOn = $('#prepared_on');
var vPreparedOn = nuFORM.removeFormatting(oPreparedOn.val(), oPreparedOn.attr('data-nu-format'));
d = new Date(vPreparedOn);
var vExpiry = d.addDays(Number($('#shelf_life').val()));
$('#calc_expiry').val(vExpiry).change();
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Re: JS Adding Days to Dates?

Unread post by pmjd »

Thanks for the code kev1n, will try and get it working :)
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Re: JS Adding Days to Dates?

Unread post by pmjd »

Hi kev1n,

I've tried putting this code on the form, and also in the prepared date input as a oninput event but neither seems to trigger the code to work. Is there something I'm missing?
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 446 times
Contact:

Re: JS Adding Days to Dates?

Unread post by kev1n »

Try replacing this line:

Code: Select all

$('#calc_expiry').val(vExpiry).change();
with:

Code: Select all

nuSetDateValue('calc_expiry',vExpiry);
You do not have the required permissions to view the files attached to this post.
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

Re: JS Adding Days to Dates?

Unread post by pmjd »

Fantastic, all working now
Post Reply