Page 1 of 1

JS Adding Days to Dates?

Posted: Mon Jul 26, 2021 4:25 am
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

Re: JS Adding Days to Dates?

Posted: Mon Jul 26, 2021 6:40 am
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();

Re: JS Adding Days to Dates?

Posted: Tue Jul 27, 2021 5:49 pm
by pmjd
Thanks for the code kev1n, will try and get it working :)

Re: JS Adding Days to Dates?

Posted: Thu Jul 29, 2021 11:49 am
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?

Re: JS Adding Days to Dates?

Posted: Thu Jul 29, 2021 12:10 pm
by kev1n
Try replacing this line:

Code: Select all

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

Code: Select all

nuSetDateValue('calc_expiry',vExpiry);

Re: JS Adding Days to Dates?

Posted: Fri Jul 30, 2021 1:08 pm
by pmjd
Fantastic, all working now