Convert a PDF Form Date from YYYY-MM-DD to MM-DD-YYYY with JavaScript
Question details
Convert a date entered by a script into a PDF form from YYYY-MM-DD to MM-DD-YYYY.

- Product
- PDF forms with JavaScript
- Device & OS
- not provided
- Scenario
- A script populates a PDF form date field, but the form needs to display the date in month-day-year order.
- Observed behavior
- Changing the field's built-in date format does not display the scripted value in the required format.
Open the form in an editor that supports Acrobat form JavaScript, such as Adobe Acrobat Pro, and identify the script and field that receive the original date. Check that the source value is consistently supplied as YYYY-MM-DD.
Use a hidden source field and a calculated display field
Use this method when you can edit the form and redirect the existing script to a separate source field.
The source field keeps the original date string, while a calculation script validates its components and writes the rearranged date to a visible field. This relies on a PDF viewer that supports Acrobat form JavaScript.
In Adobe Acrobat Pro, open All tools > Prepare a form. Add a text field named DateSource, open its Properties, and set its visibility to Hidden. On the Format tab, select None so the original YYYY-MM-DD string is retained.
Add a visible text field named DateDisplay. In its Properties, select None on the Format tab and make the field read-only if users should not edit the calculated result.
In DateDisplay Properties, open Calculate, select Custom calculation script, and click Edit. Paste this script: (function () { var source = this.getField("DateSource"); var value = source ? String(source.value) : ""; var match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value); event.value = ""; if (!match) return; var year = Number(match[1]); var month = Number(match[2]); var day = Number(match[3]); if (year < 1 || month < 1 || month > 12) return; var leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); var days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; if (day < 1 || day > days[month - 1]) return; event.value = match[2] + "-" + match[3] + "-" + match[1]; }).call(this);
Change the existing script's field assignment to write the original date to this.getField("DateSource").value instead of DateDisplay. Save the PDF and test it in Acrobat: 2025-03-09 should display as 03-09-2025, while an invalid date such as 2025-02-30 should leave the display blank.

Work with PDF forms and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with PDFs and Office documents. It supports common Microsoft Office formats for easier migration, but it should not be relied on to run or edit the Acrobat JavaScript calculation described above.

Frequently Asked Questions
Why doesn't the built-in date format fix the scripted value?
A field's date format may not interpret a scripted YYYY-MM-DD string as intended. Converting the validated components in JavaScript gives the display field an explicit MM-DD-YYYY value.
Can I change the original script instead of adding two fields?
Yes. In the script that assigns the field value, parse and validate the year, month, and day, then assign the components in MM-DD-YYYY order. Set the destination text field's Format to None if another format would alter the result.
What happens if the source date is invalid?
The example calculation leaves DateDisplay blank if the value does not match YYYY-MM-DD or if the month and day do not form a valid calendar date.
Will the calculated date work in every PDF viewer?
No. The viewer must support the Acrobat form JavaScript used by the calculation. Test the saved form in the viewer that recipients will use.




