Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


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

New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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?? 

TOPICS
Create PDFs, Edit and convert PDFs, How to, JavaScript, PDF forms

Views

1.3K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jun 07, 2021 Jun 07, 2021
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.

Likes

Translate

Translate
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

You need to check if fields are empty:

if(cTime1 == "" || cTime2 == "" || cTime3 == "" || cTime4 == "" || cTime5 == "" || cTime6 == "" || cTime7 == "" || cTime8 == "" || cTime9 == "")
event.value = "";
else
event.value = sTotalTime;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Sorry Nesa,

 

Gone down a rabbit hole here...still no result showing. 

 

// 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;

 

if(cTime1 == "" || cTime2 == "" || cTime3 == "" || cTime4 == "" || cTime5 == "" || cTime6 == "" || cTime7 == "" || cTime8 == "" || cTime9 == "")
event.value = "";
else
event.value = sTotalTime;

 

// 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 sTotalTime = nHours + ":" + nMinutes;


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

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

You place it in wrong line, 🙂 try like this:

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;

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 nTotalMinutes = nMinutes1 + nMinutes2 + nMinutes3 + nMinutes4 + nMinutes5 + nMinutes6 + nMinutes7 + nMinutes8 + nMinutes9;
var nHours = Math.floor(nTotalMinutes/60);
var nMinutes = nTotalMinutes%60;
var sTotalTime = nHours + ":" + util.printf("%,102.0f", nMinutes);

if(cTime1 == "" || cTime2 == "" || cTime3 == "" || cTime4 == "" || cTime5 == "" || cTime6 == "" || cTime7 == "" || cTime8 == "" || cTime9 == "")
event.value = "";
else
event.value = sTotalTime;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

OK Well the NaN;Nan has gone...thank you! but the total is still blank. 

 

Could it be concerned with the formulas in the cells to find the Flight Time(1-9)? 

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

It will be blank until all fields are filled.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Hmm tried that still not working. See the pic. It may be the case that I dont have 9 fields though, could just be 1 entry, or could be up to 9...either way I need to know the total time. I've used code before to find out the Flight Times (1-9) (from take off and landing) and that works good, but it isnt totalling.. Picture1.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Check that your fields are named "Flight Time1-Flight Time9" and try this code as custom calculation script of "Total Time" field:

var nMinutes1 = 0;
for( var i=1; i<=9; i++){
if(this.getField("Flight Time"+i).valueAsString != ""){
var aTime1 = this.getField("Flight Time"+i).valueAsString.split(":")
nMinutes1 += Number(aTime1[0])*60 + Number(aTime1[1]);}

var nTotalMinutes = nMinutes1;
var nHours = Math.floor(nTotalMinutes/60);
var nMinutes = nTotalMinutes%60;
var sTotalTime = nHours + ":" + util.printf("%,102.0f", nMinutes);
if(nMinutes1 == 0)
event.value = "";
else
event.value = sTotalTime;}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Ok well its registering the firstr line, but not the others. checked names, all match..

 

Picture12.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

in your photo only first line is filled, so what exactly is wrong?

It would be best if you could share file so I can see whats going on.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Ok sure, attached. 

 

I dont need all 9 to be filled to calculate total. Even if 1, or 2, or 3 lines it should make a total with the others blank if thats the case. 

 

See what you think..

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

I've adjusted the code given by Nesa so that it would add up the fields, no matter how many of them are filled in. Try this:

 

var nTotalMinutes = 0;
for (var i=1; i<=9; i++){
	if (this.getField("Flight Time"+i).valueAsString != "") {
		var aTime1 = this.getField("Flight Time"+i).valueAsString.split(":");
		var nMinutes1 = Number(aTime1[0])*60 + Number(aTime1[1]);
		nTotalMinutes += nMinutes1;
	}
}

if (nTotalMinutes == 0) {
	event.value = "";
} else {
	var nHours = Math.floor(nTotalMinutes/60);
	var nMinutes = nTotalMinutes%60;
	var sTotalTime = nHours + ":" + util.printf("%,102.0f", nMinutes);
	event.value = sTotalTime;
}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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. 23.png

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Ahhhhhh.....it is working!!! Thank you!!! Thank you!!!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

LATEST

Yes, you need to fix the fields calculation order. Also, I would revise the code that calculates the Flight Time fields. "1:0" is a strange result. It should be "1:00".

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines