Skip to main content
Participating Frequently
Sep 15, 2021 2:47 AM UTC
Answered

Timecard with auto calculate for hours worked deducting lunch

This is what i have this far. 

Field Names:

1 - Clock In, 2 - Lunchout, 3 - Lunchin, 4 - Clock out

If you use whole hours such as 08:00, 12:00, 13:00, 17:00 - works great

but if you do this 

08:00, 12:30, 13:00, 17:00 or 08:00, 12:00, 13:30, 17:00 - error calculating. What am I missing!!! I have been workign on this hours and cant seen to fix it. Any help would be greatly appriciated.

------------------------------------------------------------------------------------------

var start = this.getField("1").value;

 

var finish = this.getField("4").value;

 

if (start=="" || finish=="") event.value = "";

 

else {

 

    var lunchout = this.getField("2").value;

 

    var lunchin = this.getField("3").value;

 

    var startArr = start.split(":");

 

    var finishArr = finish.split(":");

 

    var hourWDiff = Math.abs(finishArr[0] - startArr[0]);

 

    var minWDiff = (Math.abs(finishArr[1] - startArr[1])/60*100);

 

    var hourLDiff = 0;

 

    var minLDiff = 0;

 

    if (lunchout!="" && lunchin!="") {

 

        var lunchoutArr = lunchout.split(":");

 

        var lunchinArr = lunchin.split(":");

 

        hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);

 

        minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1])/60*100);  

 

    }

 

    var totalHrs = hourWDiff + (minWDiff/60) + hourLDiff + (minLDiff/60);

 

    event.value = totalHrs;

 

}

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Sep 15, 2021 1:41 PM UTC

Thanl you! it is now deducting the lunch but not calculating to correct time. using these times it comes out incorrect. 08:00, 12:00, 13:30, 17:00 = 7.16 (should be 7.5).

Clock-In = 1

Lunch out = 2

Lunch in = 3

Clock-out = 4

------------------------------------------------------------------------------------------------

var start = this.getField("1").value;

 

var finish = this.getField("4").value;

 

if (start=="" || finish=="") event.value = "";

 

else {

 

    var lunchout = this.getField("2").value;

 

    var lunchin = this.getField("3").value;

 

    var startArr = start.split(":");

 

    var finishArr = finish.split(":");

 

    var hourWDiff = Math.abs(finishArr[0] - startArr[0]);

 

    var minWDiff = (Math.abs(finishArr[1] - startArr[1])/60*100);

 

    var hourLDiff = 0;

 

    var minLDiff = 0;

 

    if (lunchout!="" && lunchin!="") {

 

        var lunchoutArr = lunchout.split(":");

 

        var lunchinArr = lunchin.split(":");

 

        hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);

 

        minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1])/60*100); 

 

    }

 

    var totalHrs = hourWDiff + (minWDiff/60) - (hourLDiff + (minLDiff/60));

 

    event.value = totalHrs;

 

}


Change that line to:

 

var totalHrs = hourWDiff + (minWDiff/100) - (hourLDiff + (minLDiff/100));

23 replies

Bernd Alheit
Community Expert
Community Expert
Sep 15, 2021 5:37 AM UTC

Why does you add the lunch time to the working time?

Translate
Participating Frequently
Sep 15, 2021 1:09 PM UTC

Im wanting to subtract the lunch. But cant seem to get it to calculate.

Translate
try67
Community Expert
Community Expert
Sep 15, 2021 1:13 PM UTC

But you're adding it in your code...

Change this line:

var totalHrs = hourWDiff + (minWDiff/60) + hourLDiff + (minLDiff/60);

To:

var totalHrs = hourWDiff + (minWDiff/60) - (hourLDiff + (minLDiff/60));

Translate
Bernd Alheit
Community Expert
Community Expert
Sep 15, 2021 5:04 AM UTC

What error does you get?

Translate
Participating Frequently
Sep 15, 2021 5:11 AM UTC

it displys the incorrect time. 

Example:

08:00, 12:00, 13:00, 17:00 = 8.0

08:00, 12:00, 13:30, 17:00 = 8.5 (needs to be 7.5)

I have tried rearranging the equations and it still mis calculated.

Translate