Copy link to clipboard
Copied
I have a PDF form that needs to have Mon-Fri dates filled in after entering the last date in the pay period, like this:
Field name> "Period Ending" > The user enters "4/15/2016" (This is the date on Friday)
Now it needs to auto fill fields for Mon, Tue, Wed, Thu, Fri. And these fields need to be formatted as 4/11, 4/12, 4/13, 4/14, 5/15
I tried copying the Period Ending field for the "Fri" field and changing the date format, but I keep getting a format error.
I assume there is a way to get the date from the entered field and just subtract 1,2,3,4 for the other dates, which is how I would do it in Excel. Can't figure out a simple way to do it in Acrobat.
You may want to learn a bit more about how Javascript is used in the PDF environment. The one resource I am familiar with that only discusses JavaScript and Acrobat is this book: Beginning JavaScript for Adobe Acrobat
There are different ways you can do this calculation. What I like to do is to consolidate all calculations in one calculation script, which I then store as a document level function, and then call from one hidden and read-only field in the form.
You can also adjust the script slightl
...Copy link to clipboard
Copied
Working with dates using JS is not so simple. A good place to learn about it is here:
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-3
Copy link to clipboard
Copied
There are two parts to the solution, one is Acrobat specific, but the bigger part is just plain old JavaScript. You need to get familiar with JavaScript's "Date" object and how you can manipulate it. The smaller part - the Acrobat specific part - is just how to get and set form fields and how to parse and format dates.
Let's assume you have a field named "TheDate", which is where your users enter the date for Friday in the "mm/dd/yyyy" format, and then other fields for "Monday" ... "Friday" (and to make things easier they are also named that way). Then, the following script will calculate, format and assign the Friday/Thursday/Wednesday values:
var theDate = util.scand("mm/dd/yyyy", this.getField("TheDate").valueAsString);
if (theDate != null) {
// format the date as mm/dd and assign it to the "Friday" field
this.getField("Friday").value = util.printd("mm/dd", theDate);
// subtract one day and assign to Thursday
theDate.setDate(theDate.getDate() - 1);
this.getField("Thursday").value = util.printd("mm/dd", theDate);
// subtract one day and assign to Wednesday
theDate.setDate(theDate.getDate() - 1);
this.getField("Wednesday").value = util.printd("mm/dd", theDate);
}
You can add the remaining days on your own. The key here is the setDate/getDate constructs, which allow you to subtract or add a certain number of days from a date.
Copy link to clipboard
Copied
Thanks for that. Now, my question is where to add the JavaScript to the PDF? Do I put in at the document level or put each field's script into just that field? I've been going through the JavaScript tutorials, but they are little lacking in how to actually apply to PDFs.
Copy link to clipboard
Copied
You may want to learn a bit more about how Javascript is used in the PDF environment. The one resource I am familiar with that only discusses JavaScript and Acrobat is this book: Beginning JavaScript for Adobe Acrobat
There are different ways you can do this calculation. What I like to do is to consolidate all calculations in one calculation script, which I then store as a document level function, and then call from one hidden and read-only field in the form.
You can also adjust the script slightly and use it as the custom calculation script for the "Friday" field. replace the line that assigns the value to the "Friday" field with this:
event.value = util.printd("mm/dd", theDate);
Copy link to clipboard
Copied
That worked (once I found and fixed the random uppercase T in the first line.)
I put it in the "theDate" field as an action on blur.
Thanks!
Copy link to clipboard
Copied
Yes, my field was named with a capital T
Find more inspiration, events, and resources on the new Adobe Community
Explore Now