How to Calculate Dates and Times in PDF Forms Using JavaScript
Question details
Users need a way to perform calculations with dates and times dynamically inside a PDF form, such as adding or subtracting days, applying hour offsets, or counting the weeks between two specific dates.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that automatically computes future dates, past dates, time offsets, or duration (like weeks elapsed) based on user input or the current system date.
- Observed behavior
- Standard PDF form fields do not natively calculate date and time differences. JavaScript functions like util.scand and util.printd must be used to convert date strings to milliseconds, perform the math, and format the output back into the correct date/time pattern.
Ensure you have access to a PDF editor with form field calculation capabilities and basic familiarity with Adobe JavaScript for Acrobat API methods like util.scand.
Calculate a Future or Past Date by Adding or Subtracting Days
Convert the base date into milliseconds, add or subtract the required number of days, and format the output back into a date string.
PDF forms read dates as text strings. To do math on a date, you must parse the string into a JavaScript Date object using the util.scand function. Once converted, you can manipulate the time in milliseconds (1 day = 86,400,000 milliseconds).
Right-click the target date field where the result should appear, select 'Properties', and navigate to the 'Calculate' tab.
Select 'Custom calculation script' and click the 'Edit' button to open the JavaScript editor.
Read the source date field using util.scand. For example: var d = util.scand('mm/dd/yyyy', this.getField('StartDate').value);
Convert the required number of days into milliseconds. To subtract 7 days: var newDateMs = d.valueOf() - (7 * 86400000);
Convert the new millisecond value back to a date string using util.printd. For example: event.value = util.printd('mm/dd/yyyy', new Date(newDateMs));

Calculate the Number of Weeks Between Two Dates
Subtract a fixed past date from today's date and divide by the number of milliseconds in a week to get a continuous week count.
Calculate Times by Applying an Hour Offset
Apply an hour offset to a start time to automatically generate end times or timezone conversions in your PDF form.
Easily Create and Edit PDF Forms with WPS Office
Handling interactive PDF forms and editing form fields is simple with WPS Office. Whether you need to fill out complex forms, add new input fields, or manage your documents, WPS Office provides a complete, lightweight, and user-friendly PDF toolkit.
- 1. Open WPS Office: Launch WPS Office and navigate to the PDF tab.
- 2. Load your PDF Form: Open the interactive PDF form you wish to fill or edit.
- 3. Edit or Fill Forms: Click on the form fields to input your data or navigate to the 'Form' tab to modify the document structure.
- 4. Save and Export: Save your completed PDF form securely or export it directly to your preferred folder.

Frequently Asked Questions
Why isn't my JavaScript date calculation updating in the PDF form?
This usually happens because the input fields are blank or improperly formatted when the script runs. Ensure that the date format string in util.scand exactly matches the user input, and verify the calculation order under the form's calculate properties.
How do I get the current date to update automatically when the PDF opens?
You can add a Document Level JavaScript or a Page Open action with the code: this.getField('Today').value = util.printd('mm/dd/yyyy', new Date()); This will automatically populate the field named 'Today' with the current system date.
Does util.scand work in all PDF viewers?
The util.scand and util.printd methods are part of the Acrobat JavaScript API. While they are fully supported in Adobe Acrobat and robust editors like WPS Office Pro, some basic browser-based PDF viewers or built-in OS preview tools may not execute JavaScript form calculations.




