I need help with a form that calculates hours worked.
- October 25, 2022
- 3 replies
- 14355 views
Hello
I have a form that calucates hour worked. There are two time-in and time-out sections that are added together to get the total hours worked. Everything works fine if the time-in and time-out values are in the same 12-hour period. If a worker clocks in at 8:00am and clocks out at 5:13pm the total hours worked is 9:13. All is well in the world.
Here is the problem. If a worker clocks in at say 8:00pm and clocks out at 3:00am the next morning, I get a value of -17:00. Something isn't calcuating correctly when we go from pm to am.
The code I have to calculate time in and time out is:
/*
document level functions:
Scand
AdjustMidnight
DateTime2Min
*/
// get the input fields;
var cStartDate = util.printd("dd-mmm-yyyy", new Date());
var cStartTime = this.getField("StartTime").valueAsString;
var cStartDateFormat = "dd-mmm-yyyy";
var cStartTimeFormat = "h:MM tt";
cStartTime = AdjustMidnight(cStartTime);
var cEndDate = util.printd("dd-mmm-yyyy", new Date());
var cEndTime = this.getField("EndTime").valueAsString;
var cEndDateFormat = "dd-mmm-yyyy";
var cEndTimeFormat = "h:MM tt";
cEndTime = AdjustMidnight(cEndTime);
// compute elapsed time in minutes;
event.value = 0;
// compute elapsed time ony if we have data;
if(cStartDate != "" && cStartTime != "" && cEndDate != "" && cEndTime!= "")
{
event.value = DateTime2Min(cEndDateFormat + " " + cEndTimeFormat, cEndDate + " " + cEndTime) - DateTime2Min(cStartDateFormat + " " + cStartTimeFormat, cStartDate + " " + cStartTime);
} // end date objects not null;
// final format is done through the custom format option;
Any help would be greatly appreciated. I have attached a copy of the form if anyone wants to see exactly how I have it set up. Make sure you add am or pm after the time value
ex: "8:13am" then press the tab button.
Thank you.
