Copy link to clipboard
Copied
Hello all,
so I'm running into a bit of a problem. I'm trying to see if there's a way to translate the formula (in the formula bar) in the attached pic into a javascript. I have the same 4 field on a pdf file in the exact same order, but the names are different (01, 02, 03, 04) so the "formula" for the "04" field I'm looking for should go along the lines of:
=01-03+02
If you are familiar with excel formulas you know at 04 would pop up in DATE format.
Any and all help would be GREATLY APPRECIATED cause my brain is fried after trying to look for a similar javascript for the past 5 hours. Apologies if I seem to be rambling, I'm just out of it.
Copy link to clipboard
Copied
Assuming your fields are named
"Date", "Days Requested", "Days Substracted" and "Expiration Date"
Put code in "Expiration Date".
(In "Prepare form" mode, right click on "Expiration Date" field and select properties,
then select "calculate" tab and put code in "Custom calculation script".
If you want to rename your fields thats ok, just rename them in code also.
You can also change Date format if you wish, change mm/dd/yyyy to format you required.
"Expiration Date" field should auto update.
var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var cal = sub+req;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-cal);
event.value = util.printd("mm/dd/yyyy", cDate);}
Copy link to clipboard
Copied
Also, does the javascript automatically calculate the new date? Or do I have to click on something to make the "New Expiration Date" appear? I have no idea where I'm supposed to insert this javascript so help in navigating where this script is supposed to be posted would be greatly appreciate too. I'm sorry for being so demanding, I'm still new to this pdf stuff.
Copy link to clipboard
Copied
Assuming your fields are named
"Date", "Days Requested", "Days Substracted" and "Expiration Date"
Put code in "Expiration Date".
(In "Prepare form" mode, right click on "Expiration Date" field and select properties,
then select "calculate" tab and put code in "Custom calculation script".
If you want to rename your fields thats ok, just rename them in code also.
You can also change Date format if you wish, change mm/dd/yyyy to format you required.
"Expiration Date" field should auto update.
var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var cal = sub+req;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-cal);
event.value = util.printd("mm/dd/yyyy", cDate);}
Copy link to clipboard
Copied
YOU ARE A GODDESS!!! THANK YOU SO MUCH!!! THIS WORKS PERFECTLY!!!
Copy link to clipboard
Copied
I'm sorry, I spoke too soon. The script written is subtracting both the days requested and the days subtracted. I'm trying to get it to subtract the days subtracted but add the days requested. Like:
Date - Days Subtracted + Days Requested
Copy link to clipboard
Copied
Still though, I REALLY appreciate your help!
Copy link to clipboard
Copied
Sorry about that, try this code.
var req = this.getField("Days Requested").value;
var sub = this.getField("Days Substracted").value;
var tdate = this.getField("Date").valueAsString;
if(tdate == "") event.value = "";
else{
var cDate = util.scand("mm/dd/yyyy", tdate);
cDate.setDate(cDate.getDate()-sub+req);
event.value = util.printd("mm/dd/yyyy", cDate);}
Copy link to clipboard
Copied
Oh I just changed the sub+req to sub-req and it started working perfectly. Again, thank you so much! You really saved me a lot of headache.
Copy link to clipboard
Copied
If you changed to sub-req then you get same result as you get with Nesa's first code, but you said that is not correct?
example:
Nesa's 1st code "100-(20+10)=70".
Nesa's 2nd code (where you asked for "Date - Days Subtracted + Days Requested") "100-20+10=90".
your code "100-20-10=70".
Or am I missing something here?
Copy link to clipboard
Copied
when I plugged the code in with the sub+req it was subtracting both the subtracted and days requested. When I changed it to sub-req it started doing what I needed it to so. I don't know exactly how scripts work, but the 1 change made it date-sub+req, cause before that it was doing date-sub-req.

