PDF Editing Issues

Calculate Date Differences and Expiration Colors in Acrobat Forms

Algirdas JasaitisAlgirdas Jasaitis Nov 19, 2026 868 views

Question details

Calculate signed differences between form dates and highlight negative results and expired dates.

Calculate Date Differences and Expiration Colors in Acrobat Forms
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.
Before you start

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.

Solution 1Recommended

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.

1
Prepare the fields

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.

2
Open the calculation editor

Open the DaysDifference field's Properties, select the Calculate tab, choose Custom calculation script, and click Edit.

3
Enter the script

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; }

4
Test the form

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.

Add a custom calculation script to the Acrobat form
Recalculate when the date changes: The expiration color reflects the current date when the calculation runs; it does not update continuously while the form is idle. Reopen the form or change a form field to trigger recalculation. JavaScript must be enabled in the PDF viewer.
Free Microsoft Office alternative

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.

View PDFs alongside other documents in one applicationOpen and edit compatible Microsoft Word, Excel, and PowerPoint formats, including DOCX, XLSX, and PPTXUse a familiar interface for everyday document tasksStart with a free, lightweight Office alternative
microsoft office alternative - wps office

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.