How to Compare Two PDF Form Dates using JavaScript
Question details
The user needs a way to accurately compare two date fields in a PDF form using JavaScript to trigger a validation message.
- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Setting up an interactive PDF form that validates whether a 'Decision to Treat' date occurs later than a 'Referral Date'.
- Observed behavior
- Direct string comparison fails because dates from different months or years do not evaluate chronologically when compared as raw text strings.
Ensure that your PDF form fields are correctly formatted as text fields and take note of the exact date format string (e.g., 'mm/dd/yyyy') being used in those fields.
Use util.scand to Convert Strings into Date Objects
Use the built-in util.scand method to parse the text strings into JavaScript Date objects before attempting a chronological comparison.
Directly comparing date strings in JavaScript evaluates them alphabetically rather than chronologically. For example, '02/01/2023' would be considered 'less than' '11/01/2022' simply because '0' comes before '1'.
To fix this, PDF JavaScript provides the util.scand() function, which reads a formatted date string and converts it into a standard Date object that can be mathematically compared.
Before attempting to parse the dates, check if the user has actually entered a value. Use the valueAsString property to prevent errors: `if(this.getField('ref_date').valueAsString !== '')`.
Extract the values from both fields and use util.scand to parse them. For example: `var date1 = util.scand('mm/dd/yyyy', this.getField('ref_date').valueAsString);`.
Once you have valid Date objects, use standard comparison operators. For instance: `if (decisionDate.getTime() > refDate.getTime()) { app.alert('Invalid date sequence.'); }`.

Edit and Manage Interactive PDF Forms with WPS Office
WPS PDF offers robust, easy-to-use tools for viewing, filling, and managing interactive PDF forms. Whether you need to input data, add signatures, or process complex document workflows, WPS Office handles it seamlessly.
- 1. Open WPS Office: Launch WPS Office and navigate to the PDF section from the main dashboard.
- 2. Load your PDF Form: Click 'Open' to select and load your interactive PDF form.
- 3. Fill or Edit the Form: Click directly into the form fields to enter text, select dropdown dates, or trigger automated validations.
- 4. Save your Document: Go to 'Menu' and click 'Save As' to store the completed form with all entered data intact.

Frequently Asked Questions
Why does standard string comparison fail for PDF dates?
String comparison evaluates characters alphabetically from left to right. It does not understand calendar logic. Thus, a string starting with '01' (January) will always be evaluated as smaller than '12' (December), regardless of the year attached to it.
How do I check if a PDF form date field is empty?
The safest method in PDF JavaScript is to check the valueAsString property. Use an expression like `this.getField("Date1").valueAsString == ""` to verify if the field is completely empty.
What happens if util.scand fails to parse the date?
If the date string does not match the provided format, util.scand returns a null value. Attempting to call methods like .getTime() on a null value will result in a script error, so always ensure format consistency.




