How to Use JavaScript to Compare Dates in PDF Forms
Question details
The user needs a method to compare a date entered in a PDF form against a specific qualification date to automatically display an eligibility status.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that validates an installation date and automatically outputs 'Qualified' or 'Too early for qualification' based on the timeline.
- Observed behavior
- A calculated field is required to read the entered date string, convert it to a JavaScript date object, perform the date comparison, and remain blank if no date is provided.
Ensure you are using a PDF editor that supports custom JavaScript calculations for form fields, and establish a consistent date format (e.g., mm/dd/yyyy) for user inputs.
Create a Custom JavaScript Calculation Script
Use the util.scand method in a calculated text field to convert the text string into a JavaScript Date object for accurate comparison.
To perform accurate date math in PDF forms, text inputs must first be converted into recognizable date objects. The util.scand function is built into Acrobat JavaScript specifically for this purpose.
Right-click the text field where the qualification result should appear, select 'Properties', and navigate to the 'Calculate' tab.
Select the 'Custom calculation script' radio button and click the 'Edit' button to open the JavaScript editor window.
Write JavaScript to get the value of the installation date field (e.g., var instDateStr = this.getField('InstallDate').valueAsString;). Add an 'if' statement to ensure the script only runs if instDateStr is not empty.
Inside the condition, use the util.scand method to parse the string: var instDate = util.scand('mm/dd/yyyy', instDateStr);. Do the same for your target qualification date.
Compare the two date objects using standard JavaScript operators (e.g., if (instDate > qualDate)). Assign 'Qualified' or 'Too early for qualification' to 'event.value' based on the result, and set 'event.value = "";' if the input was empty.

Manage and Fill Interactive PDF Forms with WPS Office
WPS Office provides robust PDF editing capabilities, allowing you to seamlessly open, manage, and fill interactive PDF forms that contain complex JavaScript calculations without needing expensive software.
- 1. Launch WPS PDF: Open WPS Office and select the 'PDF' tab from the main dashboard to open the PDF editor.
- 2. Open Your Form: Click 'Open PDF' and select your interactive PDF form that contains the date calculation fields.
- 3. Fill and Test: Type a date into the designated input field. WPS PDF will process the embedded calculation scripts and automatically display the qualification status.
- 4. Save Your Data: Once the form is filled correctly, click 'Menu' and 'Save As' to export your finalized document securely.

Frequently Asked Questions
Why is my calculated date returning null in the PDF form?
This typically occurs when the date format string provided to the util.scand function (such as 'mm/dd/yyyy') does not exactly match the format of the text entered by the user. Check the format settings on the input field.
How do I keep the calculation field blank if no installation date is entered?
You can wrap your core JavaScript logic in a conditional statement that checks if the input field is empty (e.g., if (instDateStr !== "")). In the 'else' block, simply set event.value = ""; to ensure the field stays blank.
Can I use this date comparison script across multiple fields?
Yes. To avoid repeating code, you can create a Document-Level JavaScript function that contains the util.scand comparison logic, and then call that function from the custom calculation script of any text field in your form.




