Skip to main content
Known Participant
Jun 7, 2021 7:25 AM UTC
Answered

Adding times in HH:MM to get total time in HH:MM

Hi community

 

I have a table of up to possibly 9 elapsed time fields (Flight Time1-9), HH:MM. I wantt to total them all up to make a total time (Total Time) in HH:MM. hereis my code. 

 

// get the value of time fields 1 - 9
var cTime1 = this.getField("Flight Time1").value;
var cTime2 = this.getField("Flight Time2").value;
var cTime3 = this.getField("Flight Time3").value;
var cTime4 = this.getField("Flight Time4").value;
var cTime5 = this.getField("Flight Time5").value;
var cTime6 = this.getField("Flight Time6").value;
var cTime7 = this.getField("Flight Time7").value;
var cTime8 = this.getField("Flight Time8").value;
var cTime9 = this.getField("Flight Time9").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]);


// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + nMinutes3 + nMinutes4 + nMinutes5 + nMinutes6 + nMinutes7 + nMinutes8 + nMinutes9;
// 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 sTotal Time = nHours + ":" + nMinutes;


event.value = sTotal Time; // fillin the field's value

// build time strings or hours, ":", and minutes with leading zero
var sTotal Time = nHours + ":" + util.printf("%,102.0f", nMinutes);

 

However, there is no result displaying! Where have I gone wrong?? 

This topic is closed to new replies. Start a new post to keep the conversation going.
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Jun 7, 2021 9:55 AM UTC

Hi Try,

That sort of works, but it only updates the total when the next lines Take Off time is entered. ie without row 5 take off time, the total doesnt update. 


Code should work but you need to change field calculation order, while in prepare form tool, click on 'More'-> field calculation order and put Total time all the way down.

18 replies

Nesa Nurani
Community Expert
Community Expert
Jun 7, 2021 7:45 AM UTC

You can't have variable name with space, change: sTotal Time to sTotalTime

Translate
Nesa Nurani
Community Expert
Community Expert
Jun 7, 2021 7:54 AM UTC

Also you have wrong order at the end , starting with this line: var sTotalTime change everything to this:

var sTotalTime = nHours + ":" + util.printf("%,102.0f", nMinutes);

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

 

Translate
Known Participant
Jun 7, 2021 8:02 AM UTC

Hi Nesa, 

 

Hmmm Ok did that, still getting Nan:Nan now

 


// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + nMinutes3 + nMinutes4 + nMinutes5 + nMinutes6 + nMinutes7 + nMinutes8 + nMinutes9;
// 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 sTotalTime = nHours + ":" + nMinutes;


var sTotalTime = nHours + ":" + util.printf("%,102.0f", nMinutes);

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

Translate