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

adding hours - updated.

John864
Registered: Nov 16 2010
Posts: 19

Hi,

I am subtracting one time from another with the code attached. However I now want to total a number of these subsequent calcualtions. I am getting an error when i try to total all these hours. The code actually refers to a change I need to make to the code (change to 'event value') but I can't seem to get it to work.

can you anyone show me the change I need?

cheers
// Time Values
var cStartTime = this.getField("Dropdown11M").value;
var cEndTime = this.getField("Dropdown12M").value;
 
// Only process if field contains a value
if((cStartTime != "") && (cEndTime != ""))
{
// Convert to Hours Decimal value
var aStartTime = cStartTime.split(":");
var nStartTime = Number(aStartTime[0]) + Number(aStartTime[1])/60;
var aEndTime = cEndTime.split(":");
var nEndTime = Number(aEndTime[0]) + Number(aEndTime[1])/60;
 
// Find Difference
var nTimeDiff = nEndTime - nStartTime;
var nHours = Math.floor(nTimeDiff );
var nMinutes = Math.floor((nTimeDiff - nHours)*60 + 0.5);
  
// If used in a calculation, may need to be changed to "event.value ="
 
this.getField("TH1M").value = util.printf("%02d:%02d",nHours,nMinutes);
 
}
else
this.getField("TH1M").value = "";

My Product Information:
Acrobat Pro 9.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Works fine for me. Maybe you could specify a bit more what the problem is? Are there any messages in the console?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
What is the error message?

What are the error messages in the JavaScript Console (+J)?In what action and field is this script located in?


George Kaiser

John864
Registered: Nov 16 2010
Posts: 19
Hi, thanks for responses....

it's not an error message as such I'm getting but incorrect results when I try and total multiple time fields.

I am using the following code to sum the results of the above code but it is not adding correctly.

If I do more than 3 lines it works fine but when I add in the fourth it starts giving incorrect answer.

// get the value of time fields 1 - 21
var cTime1 = this.getField("TH1M").value;
var cTime2 = this.getField("TH2M").value;
var cTime3 = this.getField("TH3M").value;
var cTime4 = this.getField("TH4M").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]);



// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + nMinutes3 + nMinutes4;
// 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("THM: " + sTotalAllTime);
//app.alert("THM: " + sTotalAllTime, 0, 1);

event.value = sTotalAllTime; // fillin the field's value
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Have you are you handling empty of null string fields?

If you calculating a number of fields, do you have the calculation order of the fields correct?

George Kaiser

John864
Registered: Nov 16 2010
Posts: 19
Hi, I'm not sure what you mean. Sorry.
I just copied in the code above. It works for three items but doens't when I add a forth line.
As far as I know I have the order correct, i'm just adding four fields with hours and minutes.
John864
Registered: Nov 16 2010
Posts: 19
just as a follow up - even when there are values in all fields the calcuation doesn't work.
John864
Registered: Nov 16 2010
Posts: 19
if you could suggest some code that I could add to above to sort null fields, that would be great