These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Adding up time

eabikhzer
Registered: Jul 8 2008
Posts: 29

Hello I have used this code to add up time fields and works great. However, it doesn't work if one or more of the fields are left blank. Is there any way to make it so that it ignores any empty fields?

// get the value of time fields 1 - 21
var cTime1 = this.getField("TotalTime1").value;
var cTime2 = this.getField("TotalTime2").value;
... // repeat for 3 - 20
var cTime21 = this.getField("TotalTime21").value;

// split the time fields value into an array of hours and minutes
// convert the hours to minutes and compute the total minutes as a variable
var aTime1 = cTime1.split(":");
var nMinutes1 = Number(aTime1[0])*60 + Number(aTime1[1]);
var aTime2 = cTime2.split(":");
var nMinutes2 = Number(aTime2[0])*60 + Number(aTime2[1]);
.... // repeat for 3 - 20
var aTime21 = cTime21.split(":");
var nMinutes21 = Number(aTime21[0])*60 + Number(aTime21[1]);

// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + ... nMinutes21; // fillin the 3 - 20 times
// get the whole hours from the total of all minutes
var nHours = Math.floor(nTotalMinutes/60);
// get jus the minutes less than 1 hour (60 minutes) for the total of all minutes
var nMinutes = nTotalMinutes%60;
// make a string variable of the hours, ":", and minutes
var sTotalAllTime = nHours + ":" + nMinutes;

// display a literal and total time
console.show();
console.clear();
console.println("All time total: " + sTotalAllTime);
app.alert("All time total: " + sTotalAllTime, 0, 1);

event.value = sTotalAllTime; // fillin the field's value

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You have not allowed for an inputted empty time string which can not be split into an array of 2 elements.

function sHrMin2Mins(sTime) {var fMins = 0; // time in minutes// process only if time string is not emptyif (sTime.length != 0) {var aTime = sTime.split(':'); // make array of hrs & mins// convert hrs to minutes and add to minutesfMins = (60 * aTime[0]) + Number(aTime[1]);}return fMins; // return computed mins} // end sHrMin2Hrs function var fSumMins = 0; // sum of minutesconsole.println('sum: ' + fSumMins);// loop through the fieldfor (i = 1; i < 21; i++) {fSumMins += Number(sHrMin2Mins(this.getField("TotalTime" + i).value));}var fHrs = fSumMins / 60; // get the hours out of minsvar iHrs = Math.floor(fHrs); // hrs to integervar sHrs = util.printf('%,002d',iHrs); // format hrs with leading zerovar fMins = fSumMins % 60; // get fractional minutesvar sMins = util.printf('%,002.0f', fMins); // format rounded minutes for zeros// display formatted hours and minutes with leading zerosevent.value = sHrs + ':' + sMins;

George Kaiser

eabikhzer
Registered: Jul 8 2008
Posts: 29
Thanks for the reply, I have added the code to the original one, and now it doesn't work. Here is what it looks like...

// get the value of time fields 1 - 19
var cTime1 = this.getField("TotalTime1").value;
var cTime2 = this.getField("TotalTime2").value;
var cTime3 = this.getField("TotalTime3").value;
var cTime4 = this.getField("TotalTime4").value;
var cTime5 = this.getField("TotalTime5").value;
var cTime6 = this.getField("TotalTime6").value;
var cTime7 = this.getField("TotalTime7").value;
var cTime8 = this.getField("TotalTime8").value;
var cTime9 = this.getField("TotalTime9").value;
var cTime10 = this.getField("TotalTime10").value;
var cTime11 = this.getField("TotalTime11").value;
var cTime12 = this.getField("TotalTime12").value;
var cTime13 = this.getField("TotalTime13").value;
var cTime14 = this.getField("TotalTime14").value;
var cTime15 = this.getField("TotalTime15").value;
var cTime16 = this.getField("TotalTime16").value;
var cTime17 = this.getField("TotalTime17").value;
var cTime18 = this.getField("TotalTime18").value;
var cTime19 = this.getField("TotalTime19").value;

// split the time fields value into an array of hours and minutes
// convert the hours to minutes and compute the total minutes as a variable
var aTime1 = cTime1.split(":");
var nMinutes1 = Number(aTime1[0])*60 + Number(aTime1[1]);
var aTime2 = cTime2.split(":");
var nMinutes2 = Number(aTime2[0])*60 + Number(aTime2[1]);
var aTime3 = cTime3.split(":");
var nMinutes3 = Number(aTime3[0])*60 + Number(aTime3[1]);
var aTime4 = cTime4.split(":");
var nMinutes4 = Number(aTime4[0])*60 + Number(aTime4[1]);
var aTime5 = cTime5.split(":");
var nMinutes5 = Number(aTime5[0])*60 + Number(aTime5[1]);
var aTime6 = cTime6.split(":");
var nMinutes6 = Number(aTime6[0])*60 + Number(aTime6[1]);
var aTime7 = cTime7.split(":");
var nMinutes7 = Number(aTime7[0])*60 + Number(aTime7[1]);
var aTime8 = cTime8.split(":");
var nMinutes8 = Number(aTime8[0])*60 + Number(aTime8[1]);
var aTime9 = cTime9.split(":");
var nMinutes9 = Number(aTime9[0])*60 + Number(aTime9[1]);
var aTime10 = cTime10.split(":");
var nMinutes10 = Number(aTime10[0])*60 + Number(aTime10[1]);
var aTime11 = cTime11.split(":");
var nMinutes11 = Number(aTime11[0])*60 + Number(aTime11[1]);
var aTime12 = cTime12.split(":");
var nMinutes12 = Number(aTime12[0])*60 + Number(aTime12[1]);
var aTime13 = cTime13.split(":");
var nMinutes13 = Number(aTime13[0])*60 + Number(aTime13[1]);
var aTime14 = cTime14.split(":");
var nMinutes14 = Number(aTime14[0])*60 + Number(aTime14[1]);
var aTime15 = cTime15.split(":");
var nMinutes15 = Number(aTime15[0])*60 + Number(aTime15[1]);
var aTime16 = cTime16.split(":");
var nMinutes16 = Number(aTime16[0])*60 + Number(aTime16[1]);
var aTime17 = cTime17.split(":");
var nMinutes17 = Number(aTime17[0])*60 + Number(aTime17[1]);
var aTime18 = cTime18.split(":");
var nMinutes18 = Number(aTime18[0])*60 + Number(aTime18[1]);
var aTime19 = cTime19.split(":");
var nMinutes19 = Number(aTime19[0])*60 + Number(aTime19[1]);

function sHrMin2Mins(sTime) {
var fMins = 0; // time in minutes
// process only if time string is not empty
if (sTime.length != 0) {
var aTime = sTime.split(':'); // make array of hrs & mins
// convert hrs to minutes and add to minutes
fMins = (60 * aTime[0]) + Number(aTime[1]);
}
return fMins; // return computed mins
} // end sHrMin2Hrs function

var fSumMins = 0; // sum of minutes
console.println('sum: ' + fSumMins);
// loop through the field
for (i = 1; i < 21; i++) {
fSumMins += Number(sHrMin2Mins(this.getField("TotalTime" + i).value));
}
var fHrs = fSumMins / 60; // get the hours out of mins
var iHrs = Math.floor(fHrs); // hrs to integer
var sHrs = util.printf('%,002d',iHrs); // format hrs with leading zero
var fMins = fSumMins % 60; // get fractional minutes
var sMins = util.printf('%,002.0f', fMins); // format rounded minutes for zeros
// display formatted hours and minutes with leading zeros
event.value = sHrs + ':' + sMins;

// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + nMinutes3 + nMinutes4 + nMinutes5 + nMinutes6 + nMinutes7 + nMinutes8 + nMinutes9 + nMinutes10 + nMinutes11 + nMinutes12 + nMinutes13 + nMinutes14 + nMinutes15 + nMinutes16 + nMinutes17 + nMinutes18 + nMinutes19;
// get the whole hours from the total of all minutes
var nHours = Math.floor(nTotalMinutes/60);
// get jus the minutes less than 1 hour (60 minutes) for the total of all minutes
var nMinutes = nTotalMinutes%60;
// make a string variable of the hours, ":", and minutes
var sTotalAllTime = nHours + ":" + nMinutes;
// build time strings or hours, ":", and minutes with leading zero
var sTotalAllTime = nHours + ":" + util.printf("%,102.0f", nMinutes);

event.value = sTotalAllTime; // fillin the field's value
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The code is to replace your original code. Also change the loop from 21 field to 19 fields. Your original post indicated 21 fields.

The purpose of the function is to reuse repeated code and minimize the amount of coding and to allow for coding corrections to be made with a minimum amount of rewriting.

Do you get any errors showing on the JavaScript Debugging console?

If there is an error in your code before you start the new code, the script will throw an error and stop processing any more statements.

George Kaiser