PDF Editing Issues

How to Calculate the Difference Between Two PDF Date Fields using JavaScript

Huda QurayshiHuda Qurayshi Nov 5, 2026 869 views

Question details

The user needs to accurately calculate and display the time difference in days between two date fields using a custom JavaScript calculation.

How to Calculate the Difference Between Two PDF Date Fields
Product
Adobe Acrobat PDF Forms
Device & OS
not provided
Scenario
Setting up a custom JavaScript calculation in a PDF form to subtract a start date from an end date.
Observed behavior
The custom script fails to display the difference because the date parsing format applied in the script does not match the actual string format of the date fields.
Before you start

Ensure you have the exact names of your start date, end date, and calculation fields, and verify the date format currently applied to them in your form properties.

Solution 1Recommended

Match the util.scand Parsing Pattern to the Date Fields

Update your custom JavaScript so that the date format string strictly matches the visual format used in the form fields to ensure successful date parsing.

The util.scand function in PDF JavaScript requires the format parameter to exactly mirror the input string. If your date field displays a format like '15-Jan-24', attempting to parse it with a pattern like 'mm/dd/yyyy' will cause the script to return a null value and fail the calculation.

1
Verify Field Names

Open your PDF form editor and ensure your fields are named precisely. For example, identify them as dt_start, dt_end, and txt_dateDiff.

2
Check Date Field Formats

Right-click your start and end date fields, select Properties, and check the Format tab to see the exact applied date format (e.g., dd-mmm-yy).

3
Update the Script Format

In the calculation field (txt_dateDiff), open the custom calculation script and modify the util.scand format string to match the field format perfectly.

4
Calculate the Difference

Subtract the parsed start date from the end date to get the difference in milliseconds, then divide the result by 86400000 to convert it into days.

Match the util.scand Parsing Pattern to the Date Fields
Milliseconds Conversion: Because JavaScript dates are calculated natively in milliseconds, dividing your result by 86400000 (which is 1000 ms * 60 s * 60 min * 24 hrs) accurately converts the time difference into whole days.
Powerful PDF Editing

Manage PDF Forms Easily with WPS Office

WPS Office provides robust PDF editing capabilities, making it simple to create, manage, and fill out interactive PDF forms seamlessly on any device.

  1. 1. Open PDF Document: Launch WPS Office and open your PDF form document using the built-in WPS PDF tool.
  2. 2. Access Form Tools: Navigate to the Form tab on the top ribbon to view and manage your interactive document fields.
  3. 3. Edit Field Properties: Double-click any text or date field to modify properties, input formats, and settings visually without complex coding.
Easily edit and create interactive PDF formsHigh compatibility with Adobe Acrobat PDF formatsUser-friendly interface for managing text, dropdown, and date fields
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF JavaScript calculation returning an empty result?

This usually happens when there is a mismatch in date formats. If the util.scand method cannot parse the date because the format string in the script is incorrect, it returns null, causing the calculation to silently fail.

How do I change the format of dates in a PDF form?

Enter form editing mode, right-click the specific date field, select Properties, navigate to the Format tab, select 'Date' from the category list, and choose your preferred date format from the options provided.

What does util.scand do in Acrobat JavaScript?

The util.scand method converts a formatted string into a standard JavaScript Date object. This conversion is strictly required before you can perform mathematical calculations, such as finding the elapsed time between two dates.