How to Calculate Worked Hours from Start and End Times in a PDF
Question details
The user needs to calculate the elapsed worked hours automatically based on start and end times entered into PDF form fields.

- Product
- Device & OS
- not provided
- Scenario
- Designing an interactive timesheet or tracking PDF form where time values automatically generate total worked hours.
- Observed behavior
- Time values like 0800 and 1630 cannot be subtracted directly as ordinary numbers because the result will be incorrect (e.g., 830 instead of 8.5 hours). They must be parsed and converted.
Ensure you are using a PDF editor with advanced form-building capabilities that supports custom calculation scripts, as basic calculation tools cannot process time formats.
Use a Custom JavaScript Calculation in Form Properties
Implement a custom JavaScript snippet in the result field to convert the start and end times into minutes before subtracting them.
Because standard PDF calculation features only support basic arithmetic for numbers, time formats (like 08:00 or 0800) must be converted into a common unit, such as minutes, before they can be evaluated.
Once converted, the script calculates the difference and divides it by 60 to display the elapsed time in decimal hours (e.g., 8.5 hours instead of 8 hours and 30 minutes).
Open your PDF in Form Editing mode. Ensure you have text fields properly named, for example, 'StartTime', 'EndTime', and 'TotalHours'.
Right-click the 'TotalHours' field and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties window and choose the 'Custom calculation script' option.
Click 'Edit' and enter a JavaScript snippet that retrieves the values of 'StartTime' and 'EndTime', parses the hours and minutes to calculate total minutes from midnight, subtracts the start minutes from the end minutes, and divides the result by 60.
Click 'OK' to save the script, close the properties window, and exit form editing mode to test the calculation by entering time values.

View and Fill PDF Forms with WPS Office
WPS PDF provides a comprehensive set of tools to read, edit, and fill interactive PDF forms. Whether you need to handle complex timesheets, invoices, or business documents, WPS PDF ensures a smooth and reliable experience.
- 1. Install the software: Download and install WPS Office for free from the official website.
- 2. Open the PDF form: Launch WPS PDF and open your interactive timesheet or fillable PDF form.
- 3. Fill the data: Click on the fillable fields to input your start times, end times, and other required information seamlessly.
- 4. Save and share: Save your completed document securely or export it to share with your team.

Frequently Asked Questions
Can I calculate hours without using JavaScript in a PDF?
No, standard PDF form calculations only support basic addition, subtraction, multiplication, and division of regular numbers. Time calculations require parsing hours and minutes, which mandates the use of custom JavaScript.
Why does subtracting 0800 from 1630 give 830 instead of 8.5 hours?
Without a script, the PDF treats 0800 and 1630 as standard integers. Subtracting 800 from 1630 yields 830. To get 8.5 hours, the values must be recognized as time, converted to minutes (510 total elapsed minutes), and divided by 60.
What time format is best for PDF timesheets?
The 24-hour format (military time, like 08:00 or 16:30) is the most reliable format for custom JavaScript calculations in PDFs, as it avoids the need to write complex code to parse AM and PM indicators.




