Other PDF Issues

Calculate Age in Years, Months, and Days Using Acrobat JavaScript

Chanuka GeekiyanageChanuka Geekiyanage Nov 5, 2026 869 views

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.

How to Calculate Age in Years, Months, and Days Using Acrobat JavaScript
Product
PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Access Field Properties

Right-click the text field where you want the final age to appear, select 'Properties', and navigate to the 'Calculate' tab.

2
Enable Custom Scripting

Select the 'Custom calculation script' radio button, then click the 'Edit' button to open the JavaScript editor window.

3
Parse Input Dates

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)'.

4
Calculate the Difference

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.

5
Format Output

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.

Format Consistency: If a user enters a date in a format that does not match the parameters set in your util.scand() function, the calculation will fail. Consider adding field-level validation to enforce a strict date format.
Powerful PDF Form Management

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. 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. 2. Load Your Form: Drag and drop your interactive PDF form into the workspace to immediately view fields, drop-downs, and calculation outputs.
  3. 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. 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.
Seamlessly open and interact with PDF forms containing complex fields and JavaScript.Lightweight, fast-loading interface designed for efficient document management.High compatibility with standard PDF formats ensuring form structures remain intact.All-in-one suite to easily export or extract PDF form data to Excel spreadsheets.
microsoft office alternative - wps office

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.