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.
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));
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("v2 in Minutes - v1 in Minutes = " + ((Number(v2Minutes) - Number(v1Minutes))));
2200 22 00
0600 06 00 Next Day true
v2 in Minutes - v1 in Minutes = 480
480 minutes is 8 hours.
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) ***
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.