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

java script for calculating hours

klavis
Registered: Mar 16 2011
Posts: 4

I am trying to create a form with a field that calculates total hours worked from 'time in'/'time out' fields. I need to use the h:mm tt format, and I need to use regular time as opposed to military time. Please help!

Karen Lavis

My Product Information:
Acrobat Standard 9.0, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Have you looked at Thom Parker's tutorials about date and time calculations?

You need to realize that date and time values are not numbers but a standard formatted character string (the delimiters are not considered a numeric sign nor decimal point) and will need to be converted to a number like a minute or millisecond before any calculation can be preformed.

This requires a fair amount of coding and much of that coding is repeated several times with only a minor change of a field name for value. This type of coding can be reduced by using a couple of functions to minimize the coding effort. Possible functions is to convert a time value to a single standard time unit (minutes or milliseconds) for performing the calculations.and to convert a single time unit (minutes or milliseconds) to a display format or value.

Are you trying to create a time sheet. If so, then you need to compute and display the difference for each day, and then compute the total of the daily elapsed times by converting them back to a single value, summing the values and then formatting the result for display. Note that the default time formats provided by Adobe are for time within a day and can not exceed 24 hours, since there are no more than 24 hours in a day. The computations get more numerous if you are going to provide for a lunch break and how you provide that data.

Since you are using Acrobat Standard you do not have direct access to document level script editing.





George Kaiser

klavis
Registered: Mar 16 2011
Posts: 4
Thank you for you quick response. I believe I do have document level script editing if that is where you 'add' a java script to be executed in a field. I used your java script to create today's date when you open a new document. This is not a timesheet used for payroll, rather a worksheet to calculate labor hours on a particular work order. The fields look like this:

TIME: CLOCKED IN ____________ CLOCKED OUT ____________

TOTAL HOURS WORKED ______ TAKE THIS NUMBER X4 TO GET TOTAL LOS ALLOWANCE:________

I want the form to calculate the total hours worked from the 'clocked in' 'clocked out' field entries. Once I have the total hours worked, the LOS allowance field must multiply this number X 4.

I guess what I need is a script to convert regular time into minutes, then another one to subract the 'clocked in' field from the 'clocked out' field?

In other words, I still need help!

Karen Lavis

try67
Expert
Registered: Oct 30 2008
Posts: 2398
If you're interested, I recently developed a script that does just that.
Have a look here: http://try67.blogspot.com/2011/03/acrobat-calculate-time-differences-in.php

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

klavis
Registered: Mar 16 2011
Posts: 4
Thanks, but I am looking for help; not a consultant. If I could afford to hire someone to do this for me I would. Unfortunately I cannot.

Karen Lavis

Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi klavis,

The tutorials gkaiseril mentioned from the Learning Center should be your first stop then. There are 3 in the series titled Working with Dates and Times in Acrobat JavaScript. Once you have gone through those and write some code, then this is a great place to post the code you tried and ask what's wrong if it isn't working.

Date and Time calculation questions are a very popular subject in the forums here. If you do a few searches you will find lots of code examples. Much of the time you need to modify it to fit your exact needs- again this is a good place to post code you have written and ask what isn't working.

Hope this helps,

Dimitri
www.pdfscripting.com
www.windjack.com
klavis
Registered: Mar 16 2011
Posts: 4
Thanks for your help, but I've decided to create the form in Excel.

Karen Lavis

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The custom calculation for the time worked using util.scand() could be:

function Round(nValue, nPrecision) {
// round a number a given precision
return util.printf("%,1 0." + nPrecision + "f", nValue);
} // end round

function Time2Min(cFormat, sTime) {
sTime = sTime.toLowerCase();
// adjust for 12:MM am
if( (cFormat == "h:MM tt" | cFormat == "hh:MM tt") & ( (sTime.substr(0, 2) == "12" & sTime.substr(6,1) == 'a') | (sTime.substr(0, 2) == "12" & sTime.substr(5,1) == 'a') ) )
sTime = "00" + sTime.substr(2, sTime.length - 2);

// convert a formatted time string to number of minutes since midnight
// get time object
var oTime = util.scand("mm/dd/yyyy " + cFormat, "01/01/1970 " + sTime);
// convert to milliseconds
var nTime = oTime.getTime();
// convert to milliseconds to minutes and return value
nTime = nTime / (1000 * 60);
// return round to whole minutes
return Round(nTime, 0);
} // end Time2Min

( function() {
event.value = "";
// get time out
var sOut = this.getField('TimeOut').value;
// get time in
var sIn = this.getField('TimeIn').value;
// compute if we have both values
if(sOut != "" && sIn != "") {
// adjust 12:00 am to 00:00 am
if(sIn == "12:00 am" | sIn == "12:00am")
sIn = "00:00 am";
// convert time out to minutes
var nOutMin = Time2Min("h:MM tt", sOut);
// convert time in to minutes
var nInMin = Time2Min("h:MM tt", sIn);
// compute difference
var nDiff = nOutMin - nInMin;
// convert to hours
nDiff = nDiff / 60;
// round to 2 decimal places
event.value = Round(nDiff, 2);
} // end if sOut & sIn
return true;
}) ();

Or one could use the RegEx object:

function Round(nValue, nPrecision) {
// round a number a given precision
return util.printf("%,1 0." + nPrecision + "f", nValue);
} // end round

function Time2MinRE(cTime){
// convert time string h:MM tt or HH:MM to minutes
var min = 0
if ( /(\d{0,2})[:](\d{2})[ ]?([ap])/i.test(cTime) ) {
// civilian time
if(RegExp.$1 != 12)
min = 60 * RegExp.$1; // hour not 12
min += Number(RegExp.$2);
if (RegExp.$3 == "p")
min += 12 * 60; // adjust for time after 11:59.999 am
}
else if (/(\d{0,2})[:](\d{2})/.test(cTime)) {
// military time
min = 60 * Number(RegExp.$1);
min += Number(RegExp.$2);
}

return min;
} // end function Time2MinRE

(function () {
event.value = "";
// get time out
var sOut = this.getField('TimeOut').value;
// get time in
var sIn = this.getField('TimeIn').value;
// compute only if we have data
if(sOut != "" & sIn != "") {
// convert time out to minutes
var nOutMin = Time2MinRE(sOut);
// convert time in minutes
var nInMin = Time2MinRE(sIn);
// compute difference
var nDiff = nOutMin - nInMin;
// convert to hours
nDiff = nDiff / 60;
// round to 2 decimal places
event.value = Round(nDiff, 2);
}
return;
}) ();

Note that the util.scand() method requires a time format string. Using the RegEx object methos the format of the time string is determined from the passed characters within the string

The scripts were updated to adjust for the util.scand() method not handling 12:MM am correctly, it wants the value to be 00:MM am. The RegEx method code was adjusted so upper and lower case testing of am and pm was no longer needed and the handling of the values for the hour 12 was properly handled for the am and pm occurrences.

George Kaiser