4 Replies Latest reply on Apr 10, 2016 8:31 AM by gkaiseril

# Java script to calculate total hours in 12 hour format.

OK, so I know how I would do this easily in Excel, but scripting is still beyond me.

So the equation is fairly simple:

Time In          Time out          Total Hours

6:00               4:00                   10

So the Time in will always be AM and the time Time out will always be PM.  I'm not adding AM or PM to either of the boxes, Just numbers and colon as the AM and PM are appropriately assumed for this specific paper.

In Excel I would run it as Time In - (time out + 12:00) =  X  =  Total Hours......    In this case  6 - (4 + 12) = 10 Hours.

Basically automatically changing the Time out to Military and then calculating the total hours without visibly changing the Time out to 24h.

Any help to script that would be much appreciated. There is 1 time in, 1 time out, and 1 total hours for 31 days of the month. So if underlining the part of the script I would need to change for each form fillable box wouldnt be too much to ask for, I would much appreciate it.  Wish they had a way that Excel formulas would work in a PDF. That would make things super easy for me.

• ###### 1. Re: Java script to calculate total hours in 12 hour format.

Have you considered the case where one might need to check out before noon (12:00 pm) because of an emergency?

You can convert the date time string to the JavaScript date object and then use the various properties or methods of the date object to perform the calculation.

All of this is documented in the free documentation for Acrobat and JavaScript.

If you do not want to enter the am and pm then use 24 hour civilian time.

• ###### 2. Re: Java script to calculate total hours in 12 hour format.

Like I said, It is guaranteed that the first will be am and the last pm. It is not for a kind of check in check out system that you may be thinking of. In my line of work, it never happens. Absolutely never.  The people that I submit this form to, do not allow the use of 24h time. They want 12h and don't need the am and pm because it is guaranteed to be like that anyway.

• ###### 3. Re: Java script to calculate total hours in 12 hour format.

The formula should be (Time Out + 12) - (Time In) it seems. Are the time fields set up with a particular format like HH:MM or something else?

• ###### 4. Re: Java script to calculate total hours in 12 hour format.

When I served there was no ":" in military time.

You need to split the entered strings into the hours and minutes and then reduce those values to minutes since midnight. Now adjust the out minutes by 12 hours and  subtract the in time in minutes since midnight.

You can use a custom Calculations script like:

function HHMM2Num(cValue)
{
var aValue = cValue.split(":");
return (aValue[0] * 60) + Number(aValue[1]);
}

event.value = 0;
var cIn = this.getField("In").valueAsString;
var cOut = this.getField("Out").valueAsString;
if(cIn && cOut) {
event.value = (HHMM2Num(cOut) + 720) - HHMM2Num(cIn);
}

And a custom format script of:

function Num2HHMM(nMinutes)
{
return util.printf("%,101.0f:%,102.0f", Math.floor(nMinutes / 60), nMinutes % 60);
}

event.value = Num2HHMM(event.value);

These statements are all standard Acrobat JavaScript and MDN JavaScript.

If the time period being computed includes the time event when there is a time change caused by changing to or form standard and summer (DST) time then the calculation script will not be correct And one needs to add additional code to adjust for the time change.

Your logic might have an error. The time from Noon (12:00 pm) to 12:59 pm is an afternoon time so it is within reason that an out time of 12:15 could be entered and add 12 hours to that will make the ending time in the next day.