Skip to main content
May 21, 2019
Answered

timesheet calculations

  • May 21, 2019
  • 2 replies
  • 2712 views

I know this topic has been covered extensively but I have spent too much time searching past questions/answers.  If anyone can point me to the right place I would much appreciate it. 

I am creating a timesheet in PDF form and although I can calculate the difference in starttime, lunchout, lunchin, endtime the result does not format properly.

Instead of getting say 5.5 hrs the result is 5.30 hrs.   7:00 starttime; 10:30 lunchout; 11:00 lunchin; 13:00 endtime.

Here's my code so far:

// start
var start = this.getField("Start1_1").value;
var startArr = start.split(":");

//lunchout
var lunchout = this.getField("LunchOut1").value;
var lunchoutArr = lunchout.split(":");

//lunchin
var lunchin = this.getField("LunchIn1").value;
var lunchinArr = lunchin.split(":");

// finish
var finish = this.getField("End1_1").value;
var finishArr = finish.split(":");

// difference
var hourDiff = Math.abs(lunchoutArr[0] - startArr[0])+ Math.abs(finishArr[0]-lunchinArr[0]);
var minDiff = (Math.abs(lunchoutArr[1]-startArr[1])+ Math.abs(finishArr[1]-lunchinArr[1])/60*100);
if (minDiff.toString().length == 1)
    minDiff = '0' + minDiff;

var output = hourDiff + "." + minDiff;
event.value = output;

Thanks so much for any assistance.  I really need to get this finished.

Lynda

This topic has been closed for replies.
Correct answer try67

Change this:

var output = hourDiff + "." + minDiff;

To:

var output = hourDiff + (minDiff/60);

2 replies

May 21, 2019

You are my new best friend!!! 

Thank you for saving me more time down the rat hole.

try67
try67Correct answer
Community Expert
May 21, 2019

Change this:

var output = hourDiff + "." + minDiff;

To:

var output = hourDiff + (minDiff/60);

May 21, 2019

Oy, now I need to adjust the code to allow NO lunch break.  Any suggestion?

May 22, 2019

The problem is with this line:

var minWDiff = finishArr[1] - startArr[1])/60*100);

You're missing the opening parenthesis.

You still have the it too mixed up. Separate the part that calculates the lunch time entirely from the rest of the calculation.


Thanks - I couldn't see that.

I'm not a javascript programmer and I sure appreciate your help. 

Do  you have time to suggest a solution?

// start

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

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

//lunchout

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

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

//lunchin

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

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

// finish

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

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

// difference in start/finish time

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

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

//difference in lunchin/lunchout time

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

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

if (minWDiff.toString().length == 1)

    minWDiff = '0' + minWDiff;

if (minLDiff.toString().length == 1)

    minLDiff = '0' + minLDiff;

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

var output = totalHrs;

event.value = output;