• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

timesheet calculations

New Here ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 21, 2019 May 21, 2019

Change this:

var output = hourDiff + "." + minDiff;

To:

var output = hourDiff + (minDiff/60);

Votes

Translate

Translate
Community Expert ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Change this:

var output = hourDiff + "." + minDiff;

To:

var output = hourDiff + (minDiff/60);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

You are my new best friend!!!!

Thank you so much. : )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

You need to be more specific than that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Of course, sorry.

I need to modify this code in the event that the employee does not take a lunch break.

// 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/60);
event.value = output;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Yeah, you need to modify it quite a bit. Especially the lines where you calculate the hourDiff and minDiff variables.

You should break them up into smaller calculations and then you'll be able to check if the lunch fields are empty or not, before including the results in this formula.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Thanks.  Took a stab at it: (I get an error that there's a missing ';' at line 15 but I'm not seeing the problem.

// 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 = 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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Try this:

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

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

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

else {

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

    var lunchin = this.getField("LunchIn1").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;

}

I removed some lines that seems incorrect to me, and fixed one or two other small issues, as well.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Excellent! I just needed to change the ‘+’ to ‘-‘ in the totalHrs line at the end.

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

Kudos : )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

Well, that part I didn't change...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

Im still having trouble using this code below. If you use anything outside of whole hours it breaks the calculations. Any suggestions?    Example: 1 = 08:00  2 = 12:00 3 = 13:30   4 = 17:00

Field 1 = Clock In

Feild 2 = Lunch out

Feild 3 = Lunch In

Feild 4 = Clock Out

 

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;

}

Would also be nice if it could not be military time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 14, 2021 Sep 14, 2021

Copy link to clipboard

Copied

LATEST

I recommend converting the strings to Date objects (using the scand method of the util object). That will give you full control over what format they have, and will make calculating time differences much easier.

 

If you're interested, I've actually created a (paid-for) tool that can do all of that for you. It doesn't have the lunch option built-in, but that's certainly something I can add for you. You can find it here:

https://www.try67.com/tool/acrobat-calculate-time-differences-in-a-worksheet

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

You are my new best friend!!! 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines