Calculate Date Differences and Expiration Colors in Acrobat Forms
Question details
Calculate signed differences between form dates and highlight negative results and expired dates.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form contains date fields and needs to calculate the number of days between them while indicating expiration.
- Observed behavior
- The form should show negative day differences in red, other results in black, and expired dates in red.
Open the PDF in Acrobat and check that you can edit its form fields and JavaScript. The example uses three text fields named StartDate, ExpirationDate, and DaysDifference, with dates entered as MM/DD/YYYY.
Add a custom calculation script to the Acrobat form
Use this method to calculate a signed day difference and color the result and expiration date.
The script calculates ExpirationDate minus StartDate using calendar days. A result below zero is red; zero and positive results are black. An expiration date before the current local date is red, while today and future dates are black.
In Acrobat, open All tools > Prepare a form. Create or select text fields named StartDate, ExpirationDate, and DaysDifference. Set the two date fields to use MM/DD/YYYY and leave DaysDifference as a text field.
Open the DaysDifference field's Properties, select the Calculate tab, choose Custom calculation script, and click Edit.
Paste this script into the editor and click OK: function parseDate(s) { var m = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(s); if (!m) return null; var month = Number(m[1]), day = Number(m[2]), year = Number(m[3]); if (year < 1000) return null; var d = new Date(Date.UTC(year, month - 1, day)); if (d.getUTCFullYear() !== year || d.getUTCMonth() !== month - 1 || d.getUTCDate() !== day) return null; return d; } var start = parseDate(this.getField("StartDate").valueAsString); var expiryField = this.getField("ExpirationDate"); var expiry = parseDate(expiryField.valueAsString); var now = new Date(); var today = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate()); expiryField.textColor = expiry && expiry.getTime() < today ? color.red : color.black; if (!start || !expiry) { event.value = ""; event.target.textColor = color.black; } else { var days = (expiry.getTime() - start.getTime()) / 86400000; event.value = days; event.target.textColor = days < 0 ? color.red : color.black; }
Close field Properties, click Preview in Prepare a form, and enter dates in both fields. Test blank and invalid dates, equal dates, dates in reverse order, and expiration dates before, on, and after today.

Work with PDFs and Office documents in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for viewing and editing everyday documents. Its support for common Office formats makes it easier to move existing files into WPS. Use Acrobat to create and test the form JavaScript described above; WPS is not a replacement for that scripting workflow.

Frequently Asked Questions
Why is the calculated day difference blank?
Check that StartDate and ExpirationDate both contain valid dates in MM/DD/YYYY format. The example leaves DaysDifference blank if either date is empty or invalid.
How does the script decide whether a date is expired?
It compares ExpirationDate with the current local calendar date. Dates before today are red; today and future dates are black.
Can I use different field names?
Yes. In Prepare a form, check each field's name in Properties, then replace StartDate and ExpirationDate in the script with the exact names used in your PDF.
Why might the colors not update in a PDF viewer?
The viewer must support and enable Acrobat form JavaScript. Test the form in Acrobat, and trigger a recalculation by changing a form field or reopening the PDF.




