Calculate Start and End Time Differences in an Acrobat PDF Form
Question details
Calculate the duration between user-entered start and end times in each PDF form row and total the durations.

- Product
- Adobe Acrobat PDF forms
- Device & OS
- not provided
- Scenario
- A PDF worksheet has multiple rows for start and end times and a total field at the bottom.
- Observed behavior
- Each row should display its calculated duration, including overnight shifts, and the bottom field should show the total.
Open the worksheet in an Acrobat version that supports creating and editing form fields, such as Acrobat Pro. Decide on unique field names and use 24-hour times in HH:MM format.
Use Acrobat JavaScript to calculate and total row durations
Use a custom calculation script for each row, then add the calculated hour fields in a total field.
The example returns decimal hours: 1 hour and 30 minutes appears as 1.50. An end time earlier than the start time is treated as the next day; equal times produce zero hours.
This approach assumes each shift is shorter than 24 hours. It does not distinguish a zero-length shift from a full 24-hour shift.
In Acrobat, open the PDF and select Prepare a form. Add or identify text fields named Start1, End1, and Hours1 for the first row, plus a TotalHours field for the total. Use unique names for the remaining rows.
Open the Hours1 field properties, select Calculate, choose Custom calculation script, and click Edit. Enter this script: function minutes(name) { var text = this.getField(name).valueAsString; var match = /^(\d{1,2}):(\d{2})$/.exec(text); if (!match) return null; var hours = Number(match[1]); var mins = Number(match[2]); if (hours > 23 || mins > 59) return null; return hours * 60 + mins; } var start = minutes('Start1'); var end = minutes('End1'); if (start === null || end === null) { event.value = ''; } else { if (end < start) end += 1440; event.value = (end - start) / 60; }
In each remaining Hours field's Calculate tab, add the same script and replace Start1 and End1 with that row's field names. On the Format tab, choose Number with two decimal places for each Hours field.
Open TotalHours properties, select Calculate, choose Value is the sum of the following fields, and click Pick to select all Hours fields. Set its Format tab to Number with two decimal places.
In Prepare a form, use More > Set Field Calculation Order and place the Hours fields before TotalHours. Preview the form and test a daytime shift, an overnight shift such as 22:00 to 06:00, and blank or invalid entries.

Work with PDF worksheets and time calculations in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and compatibility with common Office formats for easier migration. Use WPS PDF for PDF documents or WPS Spreadsheets to calculate shift durations; it does not set up the Acrobat JavaScript calculation described above.

Frequently Asked Questions
How are overnight shifts calculated?
The row script adds 24 hours to an end time that is earlier than the start time. For example, 22:00 to 06:00 produces 8.00 hours.
What time format should people enter?
Enter 24-hour times as HH:MM, such as 09:30 or 17:45. The example script also accepts a single-digit hour, such as 9:30, but rejects invalid hours and minutes.
Why is the total not updating after a row changes?
In Prepare a form, open More > Set Field Calculation Order and make sure the row Hours fields are calculated before TotalHours. Also check that TotalHours includes every Hours field in its Calculate settings.
Will the calculations work in every PDF viewer?
Not necessarily. Viewers vary in their support for Acrobat JavaScript. Open the saved form in the intended viewer and test the row and total calculations before distributing it.




