Skip to main content
Participating Frequently
September 15, 2021
Answered

Timecard with auto calculate for hours worked deducting lunch

  • September 15, 2021
  • 2 replies
  • 2888 views

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 has been closed for replies.
Correct answer try67

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));

2 replies

Bernd Alheit
Community Expert
Community Expert
September 15, 2021

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

Participating Frequently
September 15, 2021

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

try67
Community Expert
Community Expert
September 15, 2021

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));

Bernd Alheit
Community Expert
Community Expert
September 15, 2021

What error does you get?

Participating Frequently
September 15, 2021

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.