PDF Editing Issues

How to Compare Two PDF Form Dates using JavaScript

Anum MushtaqAnum Mushtaq Nov 2, 2026 868 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Check if the date fields are empty

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 !== '')`.

2
Convert strings to Date objects

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);`.

3
Compare the resulting objects

Once you have valid Date objects, use standard comparison operators. For instance: `if (decisionDate.getTime() > refDate.getTime()) { app.alert('Invalid date sequence.'); }`.

Use util.scand to Convert Strings into Date Objects
Format Matching is Required: The format string you provide to util.scand (like 'mm/dd/yyyy' or 'dd/mm/yyyy') must exactly match the format of the text entered by the user, otherwise the function will return null.
Comprehensive PDF Editor

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. 1. Open WPS Office: Launch WPS Office and navigate to the PDF section from the main dashboard.
  2. 2. Load your PDF Form: Click 'Open' to select and load your interactive PDF form.
  3. 3. Fill or Edit the Form: Click directly into the form fields to enter text, select dropdown dates, or trigger automated validations.
  4. 4. Save your Document: Go to 'Menu' and click 'Save As' to store the completed form with all entered data intact.
Fully compatible with standard interactive PDF forms and Adobe Acrobat formats.Lightweight software that loads large PDF documents instantly.Comprehensive PDF toolkit for filling, signing, and converting documents.Intuitive user interface that simplifies document management.
microsoft office alternative - wps office

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.