4 Replies Latest reply on Sep 17, 2017 3:17 PM by joyfulpyt

# Very Simple Overtime Calculation (based on a varied hour work week)

I have a timesheet form that adds and populates based on a varied work week.  Normal work week hours can vary by each employee.  I want to auto-populate three boxes "Total Hours", "Regular Hours" and "Overtime Hours" based on whatever is entered in the "Normal Hours" field.

ex: if "Normal Hours" = 37  and "Total Hours" = 55, "Regular Hours" should read 37, "Overtime Hours" should read 15.

ex: if "Normal Hours" = 37  and "Total Hours" = 35, "Regular Hours" should read 35, "Overtime Hours" should read 0.

This reply was spot on, except my sheet is not based on a standard 40 hour work week. It could be 35, 37 etc.

Basically, "Regular Hours" should be <= Total Hours but cannot exceed Normal Hours.

I would greatly appreciate any assistance with this.

Thanks!

• ###### 1. Re: Very Simple Overtime Calculation (based on a varied hour work week)

Sounds like Regular Hours = Normal Hours, and Overtime Hours = Total Hours - Normal Hours, or zero if the result is negative. Is that correct?

• ###### 2. Re: Very Simple Overtime Calculation (based on a varied hour work week)

Hi

Very close. Actually...

Regular Hours = Total Hours or Normal Hours, whichever is smaller.

Overtime Hours = Total Hours - Regular Hours, or zero if the result is negative.  OT is over 40 hours per week.

• ###### 3. Re: Very Simple Overtime Calculation (based on a varied hour work week)

I think I understand... Use this code as the custom calculation script of Regular Hours:

```var normalHours = Number(this.getField("NormalHours").value);
var totalHours = Number(this.getField("TotalHours").value);
event.value = Math.min(regularHours, totalHours);
```

And this one for Overtime Hours:

```var regularHours = Number(this.getField("RegularHours").value);
var totalHours = Number(this.getField("TotalHours").value);
event.value = (totalHours>40) ? Math.max(totalHours-regularHours, 0) : 0;
```
• ###### 4. Re: Very Simple Overtime Calculation (based on a varied hour work week)

Thank you! The Regular Hours code worked. The Overtime code keeps giving the entire value of TotalHours - RegularHours instead of just the OT hours over 40.

So I used the code you recommended to another user and it gives me what I need.

event.value = +this.getField("TotalHours").value > 40 ? (+this.getField("TotalHours").value - 40) : 0;

I've tried multiple scenarios to make sure it works and it does, so thanks again. I really appreciate your help!