Calculate Decimal Hours from AM/PM Times in an Acrobat Form
Question details
Calculate elapsed time between AM/PM form fields as decimal hours rounded to the nearest quarter hour.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form collects start and end times, such as 12:00 PM and 1:30 PM, and needs a calculated decimal-hours result.
- Observed behavior
- The form should display 1.50 for the example and handle an end time that falls after midnight.
Open the PDF in Acrobat with access to Prepare a Form and custom calculation scripts. Decide which fields will hold the start time, end time, and calculated result.
Add a custom calculation script to the Acrobat form
Use this method to calculate quarter-hour decimal values from two AM/PM text fields, including intervals that cross midnight.
Open the PDF in Acrobat and choose All tools > Prepare a form. Add two text fields named StartTime and EndTime, and leave them as plain text so users can enter times such as 12:00 PM.
In Prepare a form, add a text field named DecimalHours. Open its Properties, select the Calculate tab, and choose Custom calculation script > Edit.
Paste this JavaScript into the editor and confirm it: function timeMinutes(value) { var text = String(value).replace(/^\s+|\s+$/g, ""); var match = /^(\d{1,2}):(\d{2})\s*(AM|PM)$/i.exec(text); if (!match) return null; var hour = Number(match[1]); var minute = Number(match[2]); if (hour < 1 || hour > 12 || minute > 59) return null; hour = hour % 12; if (match[3].toUpperCase() === "PM") hour += 12; return hour * 60 + minute; } var start = timeMinutes(this.getField("StartTime").value); var end = timeMinutes(this.getField("EndTime").value); if (start === null || end === null) { event.value = ""; } else { var elapsed = end - start; if (elapsed < 0) elapsed += 24 * 60; event.value = (Math.round((elapsed / 60) * 4) / 4).toFixed(2); }
Select Preview in Prepare a form, enter 12:00 PM in StartTime and 1:30 PM in EndTime, and confirm that DecimalHours displays 1.50. Test an overnight interval, then save the PDF.

Work with PDFs and time calculations in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for viewing PDFs and working with time calculations in spreadsheets. Its Microsoft Office format compatibility makes it easy to bring existing files across. It does not replace the Acrobat form script above.

Frequently Asked Questions
How does the calculation round to the nearest quarter hour?
The script divides elapsed minutes by 60, then uses Math.round(hours * 4) / 4. It displays the result with two decimal places, such as 1.50.
Can the form calculate a shift that ends after midnight?
Yes. When EndTime is earlier than StartTime, the script adds 24 hours to the difference. For example, 11:00 PM to 1:00 AM returns 2.00.
Why is the calculated field blank?
Check that the fields are named StartTime and EndTime and that both values use a time such as 9:30 AM. The script leaves DecimalHours blank when either entry is missing or invalid.
What happens if the start and end times are the same?
The script returns 0.00. To represent a full-day interval or durations longer than 24 hours, add date fields and use a calculation that includes the dates.




