Use JavaScript in Adobe Acrobat to Populate a Form Field From a Date
Question details
Populate a PDF form field with the holiday or company event associated with a selected date.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form uses JavaScript to look up an event based on the value of a date field.
- Observed behavior
- Only the final event appears because successive assignments overwrite earlier results.
Open the PDF in Adobe Acrobat and check the exact names of the date field and the field that will display the event. Confirm the date field's displayed format, since the script compares its text exactly.
Use an if-else chain in the event field's calculation script
Use this method to assign one event for each matching date and clear the field when no date matches.
In a custom calculation script, event.value is the value assigned to the field being calculated. An if-else chain prevents later assignments from overwriting a match.
In Acrobat, open the PDF and select Prepare a form. Open Properties for the field that should display the holiday or company event.
On the Calculate tab, select Custom calculation script and click Edit. Replace the existing assignments with: var d = this.getField("Date").valueAsString; if (d == "January 17, 2022") event.value = "Martin Luther King Day"; else if (d == "February 21, 2022") event.value = "Presidents Day"; else if (d == "June 22, 2022") event.value = "Company Picnic"; else event.value = "";
In the script editor, replace "Date" with the exact name of your date field. Change the date strings and event names to match your form, using the same format shown in the date field.
Save the script and close the field properties. Use Preview in Prepare a form to select each date and confirm that the event field updates; also test an unmatched date to confirm the field clears.

Work With Office Documents and PDFs in WPS Office
WPS Office is a free, lightweight alternative for working with documents, spreadsheets, presentations, and PDFs. Its familiar interface and Microsoft Office format support make it easier to move existing files into a single workspace. Use Adobe Acrobat for the form's JavaScript calculation; WPS is not a fix for that Acrobat script.

Frequently Asked Questions
Why does my PDF form show only the last event?
If the script assigns event.value repeatedly, each assignment can replace the previous result. Put the date checks in an if-else chain so only the matching branch assigns the event.
Where should I add the JavaScript in Acrobat?
Open Prepare a form, open Properties for the field that will display the event, and select Calculate > Custom calculation script > Edit. The script assigns the result to that field through event.value.
Why is the event field blank when I select a date?
Check that this.getField("Date") uses the date field's exact name and that the date string in the script matches the field's displayed format. The final else branch clears the event field when no string matches.
Can I use a switch statement instead of if-else?
Yes. Read the date with this.getField("Date").valueAsString, use a switch case for each exact date string, assign event.value in each case, and add a break after each assignment. Use a default case to clear unmatched dates.




