PDF Editing Issues

Extract and Format Month Names From Dates Using Acrobat JavaScript

Olivia MillerOlivia Miller Nov 24, 2026 868 views

Question details

PDF developers need a method to create dynamic date-range headers from start and end date fields.

Extract and Format Month Names From Dates Using Adobe Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Generating an automatic text header like "January-February 2023" based on dates entered by a user into start and end fields in a PDF form.
Observed behavior
A script needs to parse the exact month names and format them correctly, adjusting output dynamically depending on whether the start and end months and years match.
Before you start

Ensure you have a PDF editor that supports form field calculation scripts, and verify the exact names and formatting requirements (e.g., dd/mm/yyyy) of your start and end date fields.

Solution 1Recommended

Implement a Custom Calculation Script

Use util.scand and util.printd within a target text field's custom calculation script to dynamically parse and format the date ranges.

By utilizing Adobe Acrobat's built-in JavaScript functions, you can read the values of your date fields, convert them into JavaScript Date objects, and compare them. The util.printd function can extract specific formatting components, such as 'mmmm' for the full month name and 'yyyy' for the full year.

1
Access Form Editing Mode

Open your PDF document and enter 'Prepare Form' mode. Right-click the text field where you want the dynamic header to appear and select 'Properties'.

2
Navigate to the Calculate Tab

In the Text Field Properties dialog, click on the 'Calculate' tab. Select the option 'Custom calculation script' and click the 'Edit' button.

3
Insert the JavaScript Code

Paste the following script into the editor: var s1 = this.getField("Day 0").valueAsString; var s2 = this.getField("Day 1").valueAsString; if (s1=="" || s2=="") event.value = ""; else { var d1 = util.scand("dd/mm/yyyy", s1); var d2 = util.scand("dd/mm/yyyy", s2); if (d1.getMonth()==d2.getMonth() && d1.getFullYear()==d2.getFullYear()) { event.value = util.printd("mmmm yyyy", d1); } else { if (d1.getFullYear()==d2.getFullYear()) { event.value = util.printd("mmmm", d1) + "-" + util.printd("mmmm", d2) + " " + util.printd("yyyy", d1); } else { event.value = util.printd("mmmm yyyy", d1) + "-" + util.printd("mmmm yyyy", d2); } } }

4
Adjust Field Names

Modify 'Day 0' and 'Day 1' in the script to strictly match the actual field names assigned to your start and end date fields. Click 'OK' to save your script.

Implement a Custom Calculation Script
Date Format Dependency: The util.scand function assumes your input fields use the 'dd/mm/yyyy' format. If your fields use a different format (like 'mm/dd/yyyy'), you must adjust the format string in the util.scand method accordingly.
Efficient PDF Forms

Manage and Edit PDF Forms Seamlessly with WPS Office

While complex JavaScript calculations require specialized tools, WPS Office provides an incredibly user-friendly PDF editor to create, read, and manage interactive PDF forms effortlessly for everyday use.

  1. 1. Open PDF File: Launch WPS Office and open your target PDF form document.
  2. 2. Access the Fill & Sign Tools: Navigate to the 'PDF Edit' or 'Form' tab to interact with form fields directly.
  3. 3. Edit and Save: Fill in the required information, add text boxes if fields are missing, and save the document while retaining high compatibility with standard PDF viewers.
Effortlessly open and fill out interactive PDF formsFully compatible with standard Adobe PDF formatsLightweight suite that replaces heavy, expensive softwareEasily add text, signatures, and annotations to your documents
microsoft office alternative - wps office

Frequently Asked Questions

Why is util.scand returning a null value in my PDF script?

This typically occurs if the text string fetched from the date field doesn't perfectly match the format specified in your util.scand function (e.g., 'dd/mm/yyyy'). Ensure that your source date fields enforce the correct formatting through their field validation properties.

How do I extract only the full month name from a date object in Acrobat?

You can use the util.printd method combined with the 'mmmm' format string. For example, using util.printd('mmmm', myDateObject) will output just the spelled-out month, such as 'January'.

Can I run this JavaScript snippet in standard or mobile PDF readers?

While the formatted output text will be visible in standard PDF viewers once calculated, executing dynamic calculation scripts via Acrobat JavaScript typically requires advanced PDF software like Adobe Acrobat Pro or specialized PDF engines. Mobile readers often restrict or ignore embedded JS calculations.