Subtract Days and Time in PDF Date Fields Using Acrobat JavaScript
Question details
The user needs to calculate a date that is a specific number of days prior to a main date field in a PDF form and display the result in a different format.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where a secondary date field automatically populates with a past date (e.g., two days prior) based on the user's input in the primary date field.
- Observed behavior
- Requires a functional Acrobat JavaScript code snippet to correctly parse the main date, subtract the appropriate number of milliseconds, and format the output for the target field.
Ensure your PDF form fields are correctly named and that you have basic familiarity with entering custom JavaScript into form field properties.
Use Custom JavaScript in the Validation Script of the Main Date Field
Trigger a calculation automatically whenever the primary date field changes to populate a secondary field with the subtracted date.
This method executes a script immediately after a user enters a date in the primary field. The script parses the input string into a Date object, subtracts the requested time in milliseconds, and formats the result for the secondary target field.
Open your PDF form in edit mode, right-click your primary date field (e.g., 'DateMAIN'), and select 'Properties'.
Navigate to the 'Validate' tab within the Properties window and select the 'Run custom validation script' option.
Click 'Edit' and enter the calculation script: `this.getField("Date-2").value = util.printd("mm/dd/yy", new Date(util.scand("mmm d, yyyy", event.value) - (1000 * 3600 * 24 * 2)));`
Ensure that the primary field's format is exactly 'mmm d, yyyy' and that your secondary field is named 'Date-2'. Click OK, close the editor, and test the form.

Edit and Fill Interactive PDF Forms with WPS Office
WPS PDF provides comprehensive tools to view, edit, and fill interactive PDF forms. Whether you need to manage dates, input fields, or secure your documents, WPS Office offers a seamless, highly compatible experience.
- 1. Open the PDF File: Launch WPS Office and open your interactive PDF document.
- 2. Access Form Tools: Navigate to the 'Form' or 'Fill & Sign' tab in the top ribbon to access the editing tools.
- 3. Fill or Edit Fields: Click on the available form fields (Text, Date, Checkboxes) to input your data or modify the layout.
- 4. Save and Export: Once your form is completely filled, save the document and export it directly as a standard PDF.

Frequently Asked Questions
Why does my JavaScript date calculation return an invalid date or a blank field?
This usually happens if the date format defined in the `util.scand` function does not perfectly match the input format of the primary field. Verify that both the JavaScript parsing string and the field's actual format settings are identical.
How do I subtract a different number of days in the PDF script?
You can adjust the multiplier at the end of the script. The script calculates days in milliseconds using `(1000 * 3600 * 24 * X)`, where X is the number of days. Change the '2' in the script to your desired number of days.
Can I use this Acrobat script for time calculation as well?
Yes, but time calculations require the time field format to be clearly defined. You must parse the time using the correct format string (e.g., 'HH:MM' or 'h:MM tt') inside the `util.scand` function for accurate calculation.




