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

Calculate time in hours and tenths using a set formula and calculating the accumulated time

athompsett
Registered: Sep 27 2011
Posts: 2
Answered

I have been working on a PDF form which has a "time in" field, "time out" field, "total time" and "accumulated time" fields. I have found a couple of forms online which I tried copying the code and it will not work. There is a formula here that they go by, i.e. 0-3 minutes = 0, 4-9 minutes + .1.
 
I found code for the conversion but am a beginner at writing code and I am getting errors in the scripting box which I don't understand.
 
Does anyone have code written that would work for the Total Time field that will calculate the time between the in and out fields and convert to a set formula? I can get the accumulated field to work.
 
I used this:
 
document.write(Math.round(0.60) + "1.0");
document.write(Math.floor(0.57) + "0.90");
document.write(Math.floor(0.51) + "0.80");
document.write(Math.floor(0.45) + "0.70");
document.write(Math.floor(0.39) + "0.60");
document.write(Math.floor(0.33) + "0.50");
document.write(Math.floor(0.27) + "0.40");
document.write(Math.floor(0.21) + "0.30");
document.write(Math.floor(0.15) + "0.20");
document.write(Math.floor(0.09) + "0.10");
document.write(Math.floor(0.03) + "0.00");
 
but do not know how to make it work in the script. I am sure I am missing code before and after? The version I am working on is 9.4.6 but is not in the drop down box below.
 
Annette

My Product Information:
Acrobat Pro Extended 9.4.3, Windows
Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
Hi,

I hope this can help :

- http://acrobatusers.com/forums/aucbb/viewtopic.php?id=27580

- http://acrobatusers.com/tutorials/2006/date_time_part1
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
Acrobat JS does not support the "document.write" method.

Will your time exceed 23 hours and 59 minutes?

You can use some user defined functions. The first function would convert the time strings to hours and decimals minutes. The second would use the returned value from the first function to compute the difference. And a third function could round the minutes from the difference to the truncated 10th of an hour. Then you only need to put the above to use in a custom calculation script.

// document level functions
function Time2Hr(cTime){
var min = 0
cTime = cTime.toLowerCase();
// civilian time
if (/(\d{0,2})[:](\d{2})[ ]?([ap])/.test(cTime)) {
if (RegExp.$3 == "p") {
RegExp.$1 = Number(RegExp.$1) + 12;
}
if(RegExp.$1 == 12 & RegExp.$3 == "a") {
min = 0;
} else {
min = 60 * Number(RegExp.$1);
}
min += Number(RegExp.$2);
} else {
// military time
if (/(\d{0,2})[:](\d{2})/.test(cTime)) {
min = 60 * Number(RegExp.$1);
min += Number(RegExp.$2);
}
}
return (min / 60);
} // end function

function TimeDiffHrs(sEndTime, sStartTime) {
var nElapsed = 0;
if(sEndTime != "" & sStartTime != "") {
// convert start time string to hours
nStart = Time2Hrs(sStartTime);
// convert end time string to hours
nEnd = Time2Hrs(sEndTime);
// compute difference
nElapsed = nEnd - nStart;
} // end non-blank end and start times
// return computed hours
return nElapsed;
}

function Round2Tenths(nValue) {
// truncate to nearest 10th
var nResult = Math.floor(nValue * 10);
// truncate result
nResult = Math.floor(nResult);
// restore decimal point and return value
return nResult / 10;
}
// end document level functions

// custom calculation script
// run script if there is a start and end time string
var sStart = this.getField("Start").value;
var sEnd = this.getField("End").value;
// compute difference in hours
var nDiffHrs = TimeDiffHrs(sEnd, sStart);
// get whole hours
var nHrs = Math.floor(nDiffHrs);
// truncate the minutes to the nearest 10 minutes
var nMins = Round2Tenths(nDiffHrs % 1);
// display formatted result
event.value = util.printf("%,0 .2f", (nHrs + nMins))
// end custom calculation script

Total elapsed time for all test would be sum of the individual elapsed times.

George Kaiser

athompsett
Registered: Sep 27 2011
Posts: 2
Hi George,

Thanks for the help! I am unsure where to insert all of the code. Does it go into the custom caluation are for the field that I want to calculate the time difference. The format for that field is number with one decimal place. I made a sample PDF but cannot figure out how to attach it to this post.

Also thier time will never exceed 23 hours and 29 minutes.

Annette


gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The functions are all document level functions, Entering Document Scripts by Thom Parker.

George Kaiser