How to Automatically Display the Year from a Full Date Field in PDF Forms
Question details
The user wants a PDF form to automatically extract a four-digit year from a primary complete date input field and display it in a separate field.

- Product
- Device & OS
- not provided
- Scenario
- Creating or editing interactive PDF forms where the year needs to be automatically parsed from a provided expiry date without altering the existing layout.
- Observed behavior
- The complete date and the year need to populate accurately across two distinct form fields based on a single user input.
Ensure you are using a PDF editor that supports form field properties and custom JavaScript calculations, and write down the exact names of the target date fields you intend to link.
Use Custom JavaScript to Extract the Year
Apply a custom calculation script to the secondary year field so it can automatically format and display the data extracted from the primary date field.
PDF forms use JavaScript to handle custom calculations. By assigning a specific script to a read-only field, you can pull the value from the primary date field, format it to show only the year, and display it instantly when the user finishes typing.
Open your document in your PDF editor and enter the 'Prepare Form' mode to view all existing interactive fields.
Ensure you have a primary Date field (e.g., named 'ExpiryDate') and a secondary text field for the year (e.g., named 'ExpiryYear').
Double-click the 'ExpiryYear' field to open its Properties dialog. Navigate to the 'General' tab and check the 'Read Only' box so users cannot manually overwrite the calculation.
Switch to the 'Calculate' tab in the same Properties window. Select 'Custom calculation script' and click the 'Edit' button.
Enter the following script, replacing field names and formats to match yours: var d = this.getField('ExpiryDate').value; if(d) event.value = util.printd('yyyy', util.scand('yyyy/mm/dd', d)); else event.value = ''; then click OK and test your form.

Create and Edit Interactive PDF Forms with WPS Office
WPS PDF provides comprehensive, easy-to-use tools to create, edit, and fill interactive PDF forms efficiently, making document processing a breeze.
- 1. Open your PDF: Launch WPS Office and open the PDF document you want to edit.
- 2. Navigate to the Form Tab: Click on the 'Form' tab located in the top navigation ribbon to access form creation tools.
- 3. Insert Form Fields: Select the 'Text Field' or 'Date Field' options and click on the page to insert the interactive elements.
- 4. Configure Field Properties: Double-click the newly inserted fields to rename them and configure properties like appearance and format.
- 5. Save the Document: Click 'Save' to preserve your interactive form fields for users to fill out.

Frequently Asked Questions
Can I format the primary date field differently, such as mm/dd/yyyy?
Yes. You can configure the primary date field to whatever format you prefer in its Properties. You just need to ensure the format string in the 'util.scand' portion of your JavaScript matches that exact layout.
Why is my calculated year field remaining blank when I fill out the date?
This usually occurs if the field names referenced in the JavaScript do not exactly match the actual form field names (which are case-sensitive), or if the format string in the script does not match your date input.
Does this calculation script work when filling out the form on a mobile device?
Custom JavaScript calculations are fully supported on desktop PDF editors like Acrobat and WPS PDF. However, many mobile PDF viewer apps have limited JavaScript support and might not automatically calculate the field values.




