How to Auto-Fill Weekly Date Fields with JavaScript in a PDF Form
Question details
The user needs to use JavaScript to automatically calculate and fill in Sunday-through-Friday date fields based on a manually selected Saturday date in a PDF form.

- Product
- PDF Form
- Device & OS
- not provided
- Scenario
- Creating an automated timesheet, weekly schedule, or tracking document where inputting the end-of-week date automatically populates the dates for the preceding days.
- Observed behavior
- Requires a script to parse an input date, mathematically subtract days, handle empty field states, and push formatted results to specific form fields.
Ensure you are using a desktop PDF editor that supports custom form field JavaScript calculations. Make a note of the exact text field names for each day of the week in your form, as the script will rely on these to populate the data.
Add a Custom Validation Script to the Trigger Field
Use JavaScript within the trigger field (e.g., the Saturday date field) to calculate the previous days and pass the values to the other fields.
By attaching a validation script to the primary date field, the PDF will automatically execute the calculation whenever a new date is entered or modified. This method utilizes Acrobat's built-in date objects and utility formatting functions.
In your PDF form editor, right-click the Saturday date field and select 'Properties' from the context menu.
Click on the 'Validate' tab within the Properties window, select 'Run custom validation script', and click the 'Edit' button.
Begin your script by capturing the entered date. Use 'util.scand("mm/dd/yyyy", event.value)' to convert the text input into a readable JavaScript Date object.
Use the 'setDate()' function on your Date object to subtract 1 to 6 days. Assign these newly calculated dates to your other form fields using 'this.getField("YourFieldName").value = util.printd("mm/dd/yyyy", dateObject);'.
Wrap your logic in an 'if' statement that checks if the trigger field is empty. If 'event.value' is empty, set the value of all weekday fields to an empty string to keep the form clean.

Manage and Fill Interactive PDF Forms with WPS Office
Once your fillable PDF is created with its custom JavaScript logic, you need a reliable and lightweight tool to interact with it. WPS Office provides an excellent PDF reader and editor to seamlessly open, fill, and save interactive PDF forms without lag.
- 1. Open Your PDF Form: Launch WPS Office, click on 'Open', and select your interactive PDF file.
- 2. Fill in the Data: Click directly into the trigger date field and enter your date. Supported field calculations will trigger automatically.
- 3. Save the Document: Click 'Menu' and select 'Save As' to export your completed form securely.

Frequently Asked Questions
Why are my auto-filled dates showing the wrong format?
This happens if the format string in your 'util.printd()' function does not match your desired output. Check the script and ensure you are using standard formatting tags like 'mm/dd/yyyy' or 'dd-mmm-yyyy'.
Will this JavaScript auto-fill work on mobile PDF readers?
Most mobile PDF viewers have limited or no support for advanced JavaScript calculations. For forms relying on scripts to auto-fill data, it is highly recommended to fill them out using a desktop PDF reader.
How do I clear the form if the user makes a mistake?
If you implemented the empty state check in your script, the user simply needs to delete the date in the main trigger field (e.g., Saturday), and the script will automatically clear the remaining auto-filled weekday fields.




