How to Calculate Months Between Dates in PDF Forms Using JavaScript
Question details
The user needs to calculate the number of whole or partial months between a StartDate and an EndDate selected via date pickers in a PDF form.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires an automatic time duration calculation based on user-selected dates.
- Observed behavior
- Requires a custom JavaScript solution to parse date values and accurately count the months, as the user is new to Acrobat JavaScript.
Ensure that your StartDate and EndDate fields use the exact same predefined date format (such as mm/dd/yyyy) in their properties to prevent parsing errors during the calculation.
Write a Custom JavaScript Calculation Loop
Extract the date values as strings, convert them into JavaScript Date objects, and use a loop to count the months until the start date matches the end date.
Calculating months is more complex than calculating days because months have variable lengths. The most reliable method is to normalize the start date to the first day of its month, then repeatedly add one month in a loop while keeping a tally.
Open your PDF form editor, right-click the text field where you want the calculated months to appear, and select 'Properties'.
Navigate to the 'Calculate' tab in the Properties dialog, select 'Custom calculation script', and click the 'Edit' button to open the JavaScript editor.
Define variables for your start and end dates by retrieving the field values as strings. Use code similar to: var strStart = this.getField('StartDate').valueAsString; var strEnd = this.getField('EndDate').valueAsString;
Use the util.scand() method to convert these strings into date objects. For example: var dStart = util.scand('mm/dd/yyyy', strStart); var dEnd = util.scand('mm/dd/yyyy', strEnd);
Set the start date to the first of its month (dStart.setDate(1)). Create a counter variable set to 0. Use a 'while' loop to increment the month of dStart by 1 and increase the counter until dStart's month and year match dEnd's.
At the end of your script, assign the counter variable to the event value so the form field displays the result: event.value = counter;

View and Manage Interactive PDF Forms in WPS Office
WPS PDF provides robust and lightweight tools for handling interactive PDF forms, reviewing calculations, and organizing document pages seamlessly.
- 1. Open your PDF document: Launch WPS Office and open your interactive PDF form.
- 2. Access form tools: Navigate to the 'Form' tab on the top ribbon to view available interactive elements.
- 3. Interact with fields: Click on the text fields and date pickers to fill in your data and trigger calculations.
- 4. Save form data: Click the Save icon or press Ctrl+S to securely save your inputted data and calculation results.

Frequently Asked Questions
Why is my PDF JavaScript returning an error or NaN?
This usually happens when the date string retrieved from the field doesn't match the format expected by JavaScript. Use the util.scand('mm/dd/yyyy', dateString) method in your PDF script instead of standard JavaScript Date.parse() to accurately convert formatted date strings.
How do I change the calculation order for multiple fields in my PDF?
If your result relies on multiple fields calculating in a sequence, open your form editing tools, locate the 'Calculation Order' setting, and move your calculation fields up or down so the date inputs are processed before the final duration result.
Can I calculate the exact difference in days instead of months?
Yes. After parsing both dates into JavaScript Date objects, subtract the start date from the end date to get the difference in milliseconds. Divide that result by 86,400,000 (the number of milliseconds in a day) and use Math.round() to get the total days.




