## Calculating times past midnight

Gene--O

Total Posts

76

Likes

3

0

Gene--O

Total Posts

76

Likes

3

0
Gene--O

29-11-2011

Hello All,

I am working on a timesheet for employees. It has 4 time fields (amIN, amOUT, pmIN, pmOUT) to allow for a lunch break. I have a script that calculates well for normal business hours. BUT, if the work shift goes past midnight, then it does not calculate correctly.

Here is what I have in FormCalc in a calculate event:

var fullTime = 0
var breakTime = 0

if (pmIN ne null and pmOUT ne null) then
if (Time2Num(amIN.formattedValue, "HH:MM") < Time2Num(pmOUT.formattedValue, "HH:MM")) then
fullTime = Abs(Time2Num(amIN.formattedValue, "HH:MM") - Time2Num(pmOUT.formattedValue, "HH:MM")) /(60 * 60 * 1000)
else
fullTime = 24 - Abs(Time2Num(pmOUT.formattedValue, "HH:MM") - Time2Num(amIN.formattedValue, "HH:MM")) /(60 * 60 * 1000)
endif

breakTime = Abs(Time2Num(pmIN.formattedValue, "HH:MM") - Time2Num(amOUT.formattedValue, "HH:MM")) /(60 * 60 * 1000)
\$ = fullTime - breakTime
else

fullTime = Abs(Time2Num(amIN.formattedValue, "HH:MM") - Time2Num(amOUT.formattedValue, "HH:MM")) /(60 * 60 * 1000)
\$ = fullTime

if (amIN == null) then \$=null else \$=(fullTime - breakTime)
endif

endif

thanks

Gene-O

View Entire Topic

Gene--O

Total Posts

76

Likes

3

0

Gene--O

Total Posts

76

Likes

3

0
Gene--O

30-11-2011

Thanks so much this goes past midnight as promised. I have one more problem with it though....

Sometimes, an employee may only work in the morning (8am until noon) and take the rest of the day off with no break. I need your script to calculate those hours as a total and not wait for all of the fields (amIN, amOUT, pmIN, pmOUT) to be filled before reaching a total hours. Here is the script again, just in case you need it for the field names:

var fullTime = 0
var breakTime = 0

if (amIN ne null and pmOUT ne null) then
if (Time2Num(amIN.formattedValue, "HH:MM") lt Time2Num(pmOUT.formattedValue, "HH:MM")) then
fullTime = Abs(Time2Num(amIN.formattedValue, "HH:MM") - Time2Num(pmOUT.formattedValue, "HH:MM")) / (60 * 60 * 1000)
else
fullTime = 24 - Abs(Time2Num(pmOUT.formattedValue, "HH:MM") - Time2Num(amIN.formattedValue, "HH:MM")) / (60 * 60 * 1000)
endif
if (amOUT ne null and pmIN ne null) then
if (Time2Num(amOUT.formattedValue, "HH:MM") lt Time2Num(pmIN.formattedValue, "HH:MM")) then
breakTime = Abs(Time2Num(pmIN.formattedValue, "HH:MM") - Time2Num(amOUT.formattedValue, "HH:MM")) /(60 * 60 * 1000)
else
breakTime = 24 - Abs(Time2Num(pmIN.formattedValue, "HH:MM") - Time2Num(amOUT.formattedValue, "HH:MM")) / (60 * 60 * 1000)
endif
endif

\$ = fullTime - breakTime
else
\$ = fullTime
endif

Can you  PLEASE put in something that will allow it to calculate after the amIN and amOUT fields are filled?

thanks so much

Gene-O