Skip to main content
Participant
June 2, 2025
Answered

Calculating Total Hours

  • June 2, 2025
  • 4 replies
  • 1137 views

Hi there,

 

I am trying to calculate the total hours for each row. The variables are StartTime, BreakStart, BreakEnd, EndTime. I have set them in properties as "Time." Now, when I try to put a formula in the "total hour" box, it does not calculate. I am not sure how to make this work. Can someone please help me?

Correct answer Nesa Nurani

Try this as custom calculation of one of the text fields (only one , it will calculate all rows):

for (var i=1; i<=14; i++) {
 var startTime = this.getField("StartTime" + i).valueAsString;
 var breakStart = this.getField("BreakStart" + i).valueAsString;
 var breakEnd = this.getField("BreakEnd" + i).valueAsString;
 var endTime = this.getField("EndTime" + i).valueAsString;

 if (!startTime || !endTime) {
  this.getField("TotalHour" + i).value = "";
   continue;}

 var sT = startTime.split(":");
 var eT = endTime.split(":");
 var t1 = (Number(sT[0]) * 60) + Number(sT[1]);
 var t3 = (Number(eT[0]) * 60) + Number(eT[1]);

 var breakMinutes = 0;
 if (breakStart && breakEnd) {
  var bS = breakStart.split(":");
  var bE = breakEnd.split(":");
  var tB1 = (Number(bS[0]) * 60) + Number(bS[1]);
  var tB2 = (Number(bE[0]) * 60) + Number(bE[1]);
  breakMinutes = tB2 - tB1;}

  var num = t3 - t1 - breakMinutes;
  var hours = Math.floor(num / 60);
  var minutes = num % 60;
  var result = hours + ":" + (minutes < 10 ? "0" + minutes : minutes);

  this.getField("TotalHour" + i).value = result;}

 

 

4 replies

Participant
October 28, 2025

That’s a great question — tracking total hours is super useful, especially when you need to include travel or commute time in your calculations.
If you often handle projects that involve travel scheduling, you might also find this drive time calculator  handy. It helps estimate how long trips take based on distance and average speed, saving time when planning detailed work logs.

 

[Third partly link has been removed by Adobe Moderator]

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
June 3, 2025

Try this as custom calculation of one of the text fields (only one , it will calculate all rows):

for (var i=1; i<=14; i++) {
 var startTime = this.getField("StartTime" + i).valueAsString;
 var breakStart = this.getField("BreakStart" + i).valueAsString;
 var breakEnd = this.getField("BreakEnd" + i).valueAsString;
 var endTime = this.getField("EndTime" + i).valueAsString;

 if (!startTime || !endTime) {
  this.getField("TotalHour" + i).value = "";
   continue;}

 var sT = startTime.split(":");
 var eT = endTime.split(":");
 var t1 = (Number(sT[0]) * 60) + Number(sT[1]);
 var t3 = (Number(eT[0]) * 60) + Number(eT[1]);

 var breakMinutes = 0;
 if (breakStart && breakEnd) {
  var bS = breakStart.split(":");
  var bE = breakEnd.split(":");
  var tB1 = (Number(bS[0]) * 60) + Number(bS[1]);
  var tB2 = (Number(bE[0]) * 60) + Number(bE[1]);
  breakMinutes = tB2 - tB1;}

  var num = t3 - t1 - breakMinutes;
  var hours = Math.floor(num / 60);
  var minutes = num % 60;
  var result = hours + ":" + (minutes < 10 ? "0" + minutes : minutes);

  this.getField("TotalHour" + i).value = result;}

 

 

JR Boulay
Community Expert
Community Expert
June 2, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
AnandSri
Community Manager
Community Manager
June 2, 2025

Hello @kelly_5551!

 

I hope you are doing well, and thanks for reaching out.

 

The issue you're experiencing—where the "Total Hours" field isn't calculating as expected in your Adobe Acrobat form—can stem from several common factors.

Field Calculation Order

In Adobe Acrobat, the sequence in which fields are calculated is crucial. If the "Total Hours" field is set to calculate before the time fields (StartTime, BreakStart, BreakEnd, EndTime) have their values, it may result in incorrect or empty outputs.

Solution:

  • Navigate to Prepare Form mode.

  • Go to More > Set Field Calculation Order.

  • Ensure that the "Total Hours" field is listed after the time input fields. See this discussion on the similar topic: https://adobe.ly/4jxgICH

Field Name Accuracy

JavaScript in Acrobat is case-sensitive. If there's a mismatch between the field names used in your script and the actual field names, calculations won't execute properly.

Solution:

  • Double-check the exact names of your fields.

  • Ensure consistency in spelling and capitalization across all references.

Time Field Formatting

The format of your time fields plays a pivotal role in calculations. Inconsistent or incorrect formats can lead to errors.

Solution:

  • Set all time-related fields (StartTime, BreakStart, BreakEnd, EndTime) to a consistent format, such as 24-hour time (HH:MM).

  • Ensure that the input matches the expected format to facilitate accurate parsing and computation.

Handling Overnight Shifts

If your time entries span across midnight (e.g., starting at 10:00 PM and ending at 2:00 AM), straightforward subtraction will yield negative durations.

Solution:

  • Implement logic in your script to account for overnight shifts.

  • For instance, if the end time is earlier than the start time, add 24 hours to the end time before performing the subtraction.

Example Script for Total Hours Calculation: Note: You may have to modify the same script as per your requirements.

 

// Get time values
var start = this.getField("StartTime").valueAsString;
var breakStart = this.getField("BreakStart").valueAsString;
var breakEnd = this.getField("BreakEnd").valueAsString;
var end = this.getField("EndTime").valueAsString;

// Function to convert time string to Date object
function parseTime(timeStr) {
    if (!timeStr) return null;
    return util.scand("HH:MM", timeStr);
}

var t1 = parseTime(start);
var t2 = parseTime(breakStart);
var t3 = parseTime(breakEnd);
var t4 = parseTime(end);

if (t1 && t2 && t3 && t4) {
    var total = (t4.getTime() - t1.getTime()) - (t3.getTime() - t2.getTime());
    var hours = total / (1000 * 60 * 60); // convert from milliseconds to hours
    event.value = hours.toFixed(2); // shows two decimal places
} else {
    event.value = "";
}

 

 

I hope this helps.

Thanks,

Anand Sri.

Participant
June 2, 2025

I put in that script and it still wont calcualte