How to Compare Three Date Fields and Return the Latest Date in Adobe Acrobat
Question details
The user needs to compare three date fields in a PDF form using JavaScript and automatically output the most recent date into a target field.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Calculating the latest date from multiple user-input date fields within an interactive PDF form.
- Observed behavior
- The script must retrieve the date string values, convert them into comparable Date objects using the same date format as the input fields, compare their timestamps, and return the most recent date.
Ensure that all the date fields in your PDF form share the exact same date format (e.g., 'mm/dd/yyyy') and note down the exact field names before applying the JavaScript calculation.
Use JavaScript and Math.max to Compare Timestamps
The most reliable method is to convert the date strings to timestamps, find the maximum value using Math.max, and return the corresponding original string.
When working with Acrobat JavaScript, the util.scand method is essential for converting string date values from PDF fields into JavaScript Date objects. By converting these Date objects into epoch timestamps using getTime(), you can easily find the highest value (the latest date).
Open your PDF form in Adobe Acrobat and enter Prepare Form mode from the tools menu.
Right-click the destination text field where the latest date should appear and select Properties.
Go to the Calculate tab, select 'Custom calculation script', and click the Edit button.
Paste your custom JavaScript utilizing util.scand to fetch the three field values and Math.max to compare their getTime() values, then assign the latest string to event.value.

Compare Date Objects with Logical Operators
You can also compare the converted Date objects directly using greater-than-or-equal-to (>=) operators if you prefer conditional logic over Math.max.
Use Custom Calculation Scripts in WPS PDF Forms
WPS Office includes a robust PDF editor that supports interactive form fields and custom JavaScript calculations. You can easily add date comparison scripts to your PDF forms to automate your workflow without needing expensive software.
- 1. Open PDF Form in WPS: Open your PDF form in WPS Office and navigate to the Form tab on the top ribbon.
- 2. Access Field Properties: Double-click the destination text field to open its Properties window.
- 3. Set Calculation Script: Switch to the Calculate tab, choose 'Custom calculation script', and input your JavaScript code.
- 4. Apply and Save: Click Close to apply the script and save your document. The form will now automatically calculate the latest date.

Frequently Asked Questions
Why does util.scand return an error or null value in my Acrobat script?
This usually happens if the date string format does not exactly match the format string provided to util.scand (for example, using 'mm/dd/yyyy' in the code when the actual field contains 'dd-mm-yyyy'). Ensure the format explicitly matches your form field's input.
How do I prevent calculation errors when the date fields are empty?
You should use an if statement at the beginning of your script to check if any field value is empty (e.g., date1 == ""). If an empty string is detected, return an empty string or a default value instead of attempting to run util.scand.
Can I compare more than three date fields using this JavaScript method?
Yes. You can retrieve as many date fields as needed, convert each of them to timestamps using util.scand, and pass all the getTime() values into the Math.max() function to easily find the latest date among all the fields.




