Calculate Age in Years, Months, and Days Using Acrobat JavaScript
Question details
The user needs a custom JavaScript calculation for an Acrobat PDF form to accurately determine the exact time elapsed (years, months, and days) between two specific date fields.

- Product
- Device & OS
- not provided
- Scenario
- Creating an automated PDF form that extracts inputs from 'Date of Birth' and 'Date of Death' fields to compute and display a highly accurate age calculation.
- Observed behavior
- The form requires complex scripting to handle variable month lengths, leap years, and specific date format parsing (e.g., mm/dd/yyyy) to output the correct exact age.
Verify that both your starting date (e.g., Date of Birth) and ending date (e.g., Date of Death or Current Date) fields use the exact same strict date format, such as 'mm/dd/yyyy', to prevent script parsing errors.
Use Custom JavaScript for Exact Date Math
Implement a custom calculation script in your target field that parses both dates, accounts for leap years, and determines the remaining years, months, and days.
Calculating precise duration between two dates requires extracting the individual day, month, and year components. The script must utilize Acrobat's util.scand() function to convert the date strings into JavaScript Date objects before performing the mathematical comparisons.
Right-click the text field where you want the final age to appear, select 'Properties', and navigate to the 'Calculate' tab.
Select the 'Custom calculation script' radio button, then click the 'Edit' button to open the JavaScript editor window.
Write JavaScript to retrieve the values of your date fields using 'this.getField("DOB").value'. Convert them to Date objects using 'util.scand("mm/dd/yyyy", dateString)'.
Add logic to subtract the start year from the end year, then adjust for completed months and days. Ensure your logic rolls back a year or month if the end day/month is earlier than the start day/month.
Assign the final formatted string (e.g., 'X Years, Y Months, Z Days') to the 'event.value' property so it displays correctly in the form field.
Calculate Age in Years Only (Simplified Method)
If exact months and days are not strictly required, calculating only the elapsed years is a much simpler and less error-prone alternative.
Easily View, Manage, and Edit PDF Forms with WPS Office
WPS Office offers a comprehensive PDF editor equipped to handle interactive PDF forms natively, streamlining how you collect data, view calculations, and edit your documents alongside standard Word and Excel files.
- 1. Open WPS PDF: Launch WPS Office and click on the 'PDF' tab from the home dashboard to access the built-in PDF reader and editor.
- 2. Load Your Form: Drag and drop your interactive PDF form into the workspace to immediately view fields, drop-downs, and calculation outputs.
- 3. Fill and Save: Type directly into the pre-configured date fields. Any embedded calculation scripts will run automatically to display the calculated results.
- 4. Export Form Data: If you need to analyze multiple records, you can quickly convert or extract the filled PDF data to a WPS Spreadsheet for further management.

Frequently Asked Questions
Why is my Acrobat JavaScript date calculation returning 'NaN'?
This occurs when the date string does not exactly match the format expected by the util.scand() function, or if the user leaves the input field empty. Ensure you add an 'if' statement to check that fields are not empty before running the math.
Can I hide the calculation if the output is 0 months or 0 days?
Yes. Within your custom JavaScript, you can use conditional (if-else) statements to check if the calculated months or days equal zero. You can then dynamically build the final event.value string to exclude those specific labels.
Does Acrobat JavaScript automatically account for leap years?
The util.scand() function creates a standard JavaScript Date object, which naturally accounts for leap years in its internal timestamp. However, when you calculate specific 'months' and 'days' elapsed manually, you must ensure your script properly calculates the day rollover based on the specific month lengths involved.




