Extract and Format Month Names From Dates Using Acrobat JavaScript
Question details
PDF developers need a method to create dynamic date-range headers from start and end date fields.

- 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.
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.
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.
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'.
In the Text Field Properties dialog, click on the 'Calculate' tab. Select the option 'Custom calculation script' and click the 'Edit' button.
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); } } }
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.

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. Open PDF File: Launch WPS Office and open your target PDF form document.
- 2. Access the Fill & Sign Tools: Navigate to the 'PDF Edit' or 'Form' tab to interact with form fields directly.
- 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.

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.




