How to Calculate the Difference Between Two PDF Date Fields using JavaScript
Question details
The user needs to accurately calculate and display the time difference in days between two date fields using a custom JavaScript calculation.

- 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.
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.
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.
Open your PDF form editor and ensure your fields are named precisely. For example, identify them as dt_start, dt_end, and txt_dateDiff.
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).
In the calculation field (txt_dateDiff), open the custom calculation script and modify the util.scand format string to match the field format perfectly.
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.

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. Open PDF Document: Launch WPS Office and open your PDF form document using the built-in WPS PDF tool.
- 2. Access Form Tools: Navigate to the Form tab on the top ribbon to view and manage your interactive document fields.
- 3. Edit Field Properties: Double-click any text or date field to modify properties, input formats, and settings visually without complex coding.

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.




