4 Replies Latest reply on Feb 25, 2015 3:39 PM by gkaiseril

# Custom calculations for time card

I have been looking for Java Script for my particular need and I can't seem to find the right one. We use a PDF form to track hours worked over various types of work codes. We use military time format in our form. I have a simple if/then scheme that will take care of the math, but I can't figure out the code for it. The major issue is when shifts cross over the midnight barrier and that's where the if/then comes in.

Easy Example:

v1 = Monday Start @ 0600

v2 = Monday End @ 1400

v2-v1 = Total time

Difficult Example:

v1 = Monday Start @ 2200

v2 = Monday Ends on Tuesday @ 0600

if/then

if v1<1200 then time = v1+2400

if v2<1200 then time = v2+2400

v2-v1/100 = Total Time

In this example the Monday start stays 2200 and the 0600 end becomes 2800 after the 2400 is added to the 0600.

Now (2800 - 2200)/100 = 6 hours

So the math is simple but the script is beyond me. Can anyone assist with this? And yes, Excel would be easier but I do not have that option. Thank you in advanced for anything you guys/gals might be able to throw at me.

• ###### 1. Re: Custom calculations for time card

The math is not that simple because the measurement of hours and minutes is not like adding dollars and cents. Instead of being a decimal number system in which one counts from 0 to 9 or the highest digit and then the 10's column starts at 10 and counts to 99 and then 100's start at 100 on goes to 999. Time is measured and computed differently. The seconds range from 0 to 59 and then the minutes is incremented and the seconds start again at 0. Minutes go from 0 to 59 and then the hour is incremented and the minutes start over at 0. So if you want to compute hours and minutes one needs to convert the unit of measurements to a common unit. Like converting the hours to minutes and then add the minutes for the starting time and then doing that for the ending time. Now one can compute the elapsed minutes from the starting time to the ending time in minutes. If you want the result in hours and minutes then you have to convert the time to whole hours and minutes less than the whole hours.

So if one starts Monday at 0600 and ends at 1400 Monday, the start value is 6 hours * 6o minutes or 360 minutes plus 0 minutes. The ending value is 14 hours * 60 minutes or 840 minutes and 0 minutes. The difference in minutes is 840 - 360 or 480 minutes or 8 hours and 0 minutes.

For your second example. The start time of 2200 is 22 hours * 60 minutes  plus 0 minutes, or 1320 minutes. The end time is 6 hours * 60 minutes plus 0 minutes or 360 plus 24 hours * 60  (1440 minutes) to adjust for the number of days since the starting day or 1800 minutes. The difference between 1800 minutes and 1320 minutes is 480 minutes or 8 hours. For check of the result, 2 hours from 2200 to 2400 and 6 hours from 0000 (2400) to 0600 totals to 8 hours.

• ###### 2. Re: Custom calculations for time card

Try the following example using your code in the JavaScript console:

var v1 = "2200";
var v2 = "0600";
if(v1 < 1200) v1 = String(Number(v1) + 2400);
if(v2 < 1200) v2 = String(Number(v2) + 2400);
console.println("v2 - v1 / 1000 = " + (Number(v2) - Number(v1) / 1000));
console.println("(v2 - v1) / 1000 = " + ((Number(v2) - Number(v1)) / 1000));

Results:

v2 - v1 / 1000 = 2997.8

(v2 - v1) / 1000 = 0.8

Same values but adding a variable to detect the end date in the next day:

// compute time in minutes;

var v1 = "2200";

var v2 = "0600";

var NextDay = true;

var v1Hrs = v1.toString().substr(0,2);

var v1Mins = v1.toString().substr(2,2);

var v1Minutes = (v1Hrs * 60) + Number(v1Mins);

var v2Hrs = v2.toString().substr(0,2);

var v2Mins = v2.toString().substr(2,2);

var v2Minutes = (v2Hrs * 60) + Number(v2Mins);

if(NextDay == true) v2Minutes = Number(v2Minutes) + (24 * 60);

console.println(v1 + " " + v1Hrs + " " + v1Mins)

console.println(v2 + " " + v2Hrs + " " + v2Mins + " Next Day " + NextDay)

console.println(v2Minutes);

console.println("v2 in Minutes - v1 in Minutes = " + ((Number(v2Minutes) - Number(v1Minutes))));

Results:

2200 22 00
0600 06 00 Next Day true
1800
v2 in Minutes - v1 in Minutes = 480

480 minutes is 8 hours.

• ###### 3. Re: Custom calculations for time card

Thank you, I will give that a shot tomorrow. I've been toying with Excel to get the math right and I have a formula that works. I was thinking of using an excel spreadsheet and then importing the data to a PDF. In Excel I set all of the columns to time format.

A1:  0:00 (midnight)

B1: 12:00 (noon)

A2: user entered in-punch

B2: user entered out-punch

A3: [=IF(A2>B1, A1-A2, A2)

*** C2: =IF(A2>B1, A3+B2, B2-A2) ***

• ###### 4. Re: Custom calculations for time card

You need to test for a lot of other values to see if your script will work.

It is not a good idea to assume about the value for the ending time being on the same day as the starting time. Time cards can be legal evidence so precision and accuracy is very important.

Also JavaScript does not have a Military 24 hour format, only the Civilian 24 hour format. It is possible to create the Military 24 hour format but there maybe additional scripts needed for keystrokes, formatting, and validation.