How to Calculate Time Differences from Hour, Minute, and AM/PM Fields in PDF Forms
Question details
The user needs to calculate the elapsed time between start and end times selected from separate hour, minute, and AM/PM dropdown fields in a PDF form.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating a time-tracking PDF document where users select start and end times using individual dropdown menus.
- Observed behavior
- Requires a custom calculation approach to convert 12-hour format dropdown selections into numeric values, compute the difference, and handle overnight shifts without errors.
Ensure your PDF editor supports JavaScript for form fields, and that you have properly named your start and end time dropdown fields to easily reference them in your calculation script.
Use a Single Custom JavaScript Calculation
Combine the conversion of AM/PM dropdowns to 24-hour decimal values and the subtraction process into one comprehensive script on the total time field.
By assigning decimal export values to minute options (e.g., 0.25 for 15 minutes) and programmatically adding 12 to PM hours, you can create continuous numeric time values. Handling this in a single script on the 'Total Time' field ensures the form remains clean and functions automatically, even for overnight shifts.
Open the properties for your minute dropdown fields. Assign numeric export values for each option (e.g., set :00 to 0, :15 to 0.25, :30 to 0.5, and :45 to 0.75).
Right-click the text field where you want the total elapsed time to appear, select Properties, and navigate to the Calculate tab.
Select 'Custom calculation script' and click Edit. Write JavaScript variables to retrieve the values of your start and end hour, minute, and AM/PM fields using the getField() method.
In your script, add an 'if' statement checking if the AM/PM field equals 'PM'. If it does, add 12 to the hour value to convert it to a 24-hour format.
Subtract the calculated start time from the end time. Add a condition that if the total result is less than 0, add 24 to the result to properly account for overnight shifts.

Use Separate Hidden Calculated Fields
Calculate the start and end times in two hidden fields first, then subtract them in a third visible field for simplified scripting.
Easily Create Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive suite of PDF tools, allowing you to easily build, edit, and configure interactive PDF forms. With support for advanced form elements and properties, creating complex time-tracking documents is intuitive and fast.
- 1. Open the PDF Form: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab in the top ribbon.
- 2. Insert Form Fields: Use the toolbar to insert Text Fields, Drop-Down menus, and Checkboxes to build your time-tracking layout.
- 3. Configure Field Properties: Double-click any form field to open its properties menu. Here, you can define export values for dropdowns and set up custom calculation scripts.
- 4. Save and Distribute: Once your fields and calculations are configured, save the interactive PDF to easily share it with users.

Frequently Asked Questions
How do I assign export values to a dropdown in a PDF form?
Right-click the dropdown field, select Properties, and go to the Options tab. Type your display item name (e.g., '15') in the Item field and specify your numeric export value (e.g., '0.25') in the Export Value field before clicking Add.
Why is my elapsed time calculation showing a negative number?
This usually happens during overnight shifts when the end time (e.g., 2:00 AM) is numerically smaller than the start time (e.g., 10:00 PM). You must add an 'if' statement in your script to add 24 to the total difference if the result is negative.
Can I use simplified field notation instead of JavaScript for this?
Simplified field notation works well for basic arithmetic (like A + B), but handling AM/PM string text conversions and applying conditions for overnight shifts strictly requires custom JavaScript calculations.




