PDF Editing Issues

How to Automatically Display the Year from a Full Date Field in PDF Forms

Anum MushtaqAnum Mushtaq Nov 5, 2026 870 views

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.

How to Automatically Display the Year from a Full Date Field in PDF Forms
Product
PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Form Preparation Mode

Open your document in your PDF editor and enter the 'Prepare Form' mode to view all existing interactive fields.

2
Create the Required Fields

Ensure you have a primary Date field (e.g., named 'ExpiryDate') and a secondary text field for the year (e.g., named 'ExpiryYear').

3
Set the Year Field to Read-Only

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.

4
Add the Calculation Script

Switch to the 'Calculate' tab in the same Properties window. Select 'Custom calculation script' and click the 'Edit' button.

5
Input the JavaScript Code

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.

Use Custom JavaScript to Extract the Year
Match Date Formats: Ensure that the date format used in the util.scand function precisely matches the format configuration of your primary 'ExpiryDate' field (e.g., 'mm/dd/yyyy' or 'yyyy/mm/dd').
Powerful PDF Form Editor

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. 1. Open your PDF: Launch WPS Office and open the PDF document you want to edit.
  2. 2. Navigate to the Form Tab: Click on the 'Form' tab located in the top navigation ribbon to access form creation tools.
  3. 3. Insert Form Fields: Select the 'Text Field' or 'Date Field' options and click on the page to insert the interactive elements.
  4. 4. Configure Field Properties: Double-click the newly inserted fields to rename them and configure properties like appearance and format.
  5. 5. Save the Document: Click 'Save' to preserve your interactive form fields for users to fill out.
Easily create interactive text, date, and dropdown fields in your PDF forms.Highly compatible with standard PDF forms and Adobe Acrobat formats.Lightweight and intuitive interface designed to boost your daily productivity.
microsoft office alternative - wps office

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.