JS Adding Days to Dates?

Questions related to customising nuBuilder Forte with JavaScript or PHP.

JS Adding Days to Dates?

Unread postby pmjd » Mon Jul 26, 2021 11:55 am

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
pmjd
 
Posts: 96
Joined: Fri Mar 12, 2021 7:08 pm
nuBuilder Version: 4.5

Re: JS Adding Days to Dates?

Unread postby kev1n » Mon Jul 26, 2021 2:10 pm

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();
kev1n
nuBuilder Team
 
Posts: 2050
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: JS Adding Days to Dates?

Unread postby pmjd » Wed Jul 28, 2021 1:19 am

Thanks for the code kev1n, will try and get it working :)
pmjd
 
Posts: 96
Joined: Fri Mar 12, 2021 7:08 pm
nuBuilder Version: 4.5

Re: JS Adding Days to Dates?

Unread postby pmjd » Thu Jul 29, 2021 7:19 pm

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?
pmjd
 
Posts: 96
Joined: Fri Mar 12, 2021 7:08 pm
nuBuilder Version: 4.5

Re: JS Adding Days to Dates?

Unread postby kev1n » Thu Jul 29, 2021 7:40 pm

Try replacing this line:

Code: Select all
$('#calc_expiry').val(vExpiry).change();


with:

Code: Select all
nuSetDateValue('calc_expiry',vExpiry);
Attachments
adddays.png
adddays.png (31.04 KiB) Viewed 275 times
kev1n
nuBuilder Team
 
Posts: 2050
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: JS Adding Days to Dates?

Unread postby pmjd » Fri Jul 30, 2021 8:38 pm

Fantastic, all working now
pmjd
 
Posts: 96
Joined: Fri Mar 12, 2021 7:08 pm
nuBuilder Version: 4.5


Return to Custom Code

Who is online

Users browsing this forum: No registered users and 8 guests

cron