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

Need Help Fixing the Code I am Using to Calculate Time

Explorer ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

Thank you for your HELP!!

Before I start...I have research this page and have tried to apply codes that have been posted and have tried to decipher them, which I humblly say I have failed and now asking for help. So as you look at my attachment you may see some scrambled codes. I'm lost and need assistance.

 

All I need, seems simple but apparently is not to me, is to be shown how many hours between the start time and the end time for each day minus 30min lunch break for each day and then total those hours across for a weekly total

I vaugly understand that the date will pose a problem if crossed. But my times should not cross over the 12 hour period, per se.

But it seems that when I put a time group in it works perfectly but when I put another time group in it doesn't calculate correctly. 

For example: 0800 start 1630 finish equals 8 hours - PERFECT

but if I use 0630 start and 1700 finish this jumpts to 11

So my calculations fields are as follows

"SunRow1" & "SunRow2" will add into "day1"  (the field day1 is a hidden field to the end user)         

Then "day1" will add the "lunch" field (which will always be -.50 (the field lunch is hidden to the end user)).

The end user will see the results in Day1ttl. The results should only be shown at a whole or half 8 or 8.5 digits. 

Then for each week the total week should show the total hours based on the day#ttl fields for each week.

 

Again Thank you any help!!

TOPICS
Acrobat SDK and JavaScript

Views

1.6K

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 , Mar 27, 2020 Mar 27, 2020

The problem is with your use of Math.abs in the minDiff calculation. Because you use it you don't know if the minutes of the start time are more or less than those of the end time.

To solve it replace this line:

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

With this:

var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
if (minDiff<0) {hourDiff--; minDiff*=-1;}

Votes

Translate

Translate
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Copy link to clipboard

Copied

There are errors in your code. Check the JS Console (Ctrl+J) after changing the value of one of the fields.

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
Explorer ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

This is the code that works decently for my needs, except for the 0630-1700 error mentioned above. Can you help me with fixing this so that basically any time (same day only) will calculate correctly?.I understand crossing over days will mess things up, but the time shouldn't cross days.

 

var start = this.getField("SunRow1").valueAsString;
var finish = this.getField("SunRow2").valueAsString;
if (start=="" || finish=="") event.value = "";
else {
var startArr = [start.substr(0,2), start.substr(2,2)];
var finishArr = [finish.substr(0,2), finish.substr(2,2)];
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);
if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;
var output = hourDiff + "." + minDiff;
event.value = output;

}

Thank you,

Teresa

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

The problem is with your use of Math.abs in the minDiff calculation. Because you use it you don't know if the minutes of the start time are more or less than those of the end time.

To solve it replace this line:

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

With this:

var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
if (minDiff<0) {hourDiff--; minDiff*=-1;}

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
Explorer ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Amazing!! Thank you so much. Worked perfectly! Be safe and take care!!

 

Thank you,

Teresa

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

I think you are overcomplicating the calculation. How about just convert the hours/minutes into single number and subtract? Forget about catching crossover points.

 

var t1 = Number(start.substr(0,2)) + Number(start.substr(2,2))/60;

var t2 = Number(finish.substr(0,2)) + Number(finish.substr(2,2))/60;

var diff = t2 - t1;

//Done!! 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

I do not know java at all.  I piece this together based on other information I have found.
I wish I could say I understand, but I don't understand what you mean by catching crossover points.
I really appreciate all your help and explanations helping me better learn this language.

 

Because I do not know how to deduct the 30min lunch hour I had to create a "lunch" field in order to be able to deduct it. Is that what you mean by crossover points?

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 ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

What I meant was that when minute and hour differences are calculated separtely, the point where the minutes cross an hour boundary (negative minutes) have to be detected and handled. The same is true for 24 hour boundaries.  The calculation is much smoother if the minutes and hours are combined, now you don't need to detect that crossover point. 

 

This is a JavaScript issue, it's about how a calculation is broken down so it can be preformed with a discrete set of steps. 

 

As for the 30minutes, that just need to be added into the calculation. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Mar 27, 2020 Mar 27, 2020

Copy link to clipboard

Copied

Thank you so much for your replies.

 

So I replaced "Number" with my fields (SunRow1,SunRow2) but it didn't calculate anything. Obviously I am not understanding your code! haha!

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 ,
Mar 28, 2020 Mar 28, 2020

Copy link to clipboard

Copied

If you don't know what something in the code does you should not change it...

This is the full code you should use, based on Thom's reply:

 

var start = this.getField("SunRow1").valueAsString;
var finish = this.getField("SunRow2").valueAsString;
if (start=="" || finish=="") event.value = "";
else {
	var t1 = Number(start.substr(0,2)) + Number(start.substr(2,2))/60;
	var t2 = Number(finish.substr(0,2)) + Number(finish.substr(2,2))/60;
	var diff = t2 - t1;
	event.value = diff;
}

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 ,
Mar 28, 2020 Mar 28, 2020

Copy link to clipboard

Copied

Thanks Try67,   

What I meant to say in the last post is that this is NOT a JavaScript issue, it is an algorithmic issue.  It's how you think about solving an issue. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Mar 28, 2020 Mar 28, 2020

Copy link to clipboard

Copied

Thank you both for these great scripts.

These work great, with the exception of I am still missing the 30min deduction I need in the calculations.

Any assistance having this added would be greatly appreciated.

Thank you,

Teesa

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 ,
Mar 28, 2020 Mar 28, 2020

Copy link to clipboard

Copied

Reduce 0.5 from the final result, or from diff, in the case of the code above.

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
Explorer ,
Mar 29, 2020 Mar 29, 2020

Copy link to clipboard

Copied

Works Perfectly! Thank you both for your help and expertise!!

Be well!!

Teresa

 

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
Explorer ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

Works perfectly!! Thank you both for your assistance! Much Appreciated!

 

Be Well!

Teresa

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
LEGEND ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

A tip, I hope you will find it helpful in your future research. You call it "Java", but it isn't. This may seem a reasonable way to shorten JavaScript, but Java is an entirely different language. So if you try to learn by Googling for Java, you get some really unhelpful stuff (which looks enough like JavaScript at first to get further tangled...)

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 ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

People, do not post replies to the long thread above. They don't show up due to a serious bug in the forums system!

See: https://community.adobe.com/t5/community-feedback/critical-bug-replies-are-disappearing/m-p/10997350

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 ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

The last two replies posted here, by tacnola and TSN, are not visible due to this bug.

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
LEGEND ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

Curiously, I do see my reply.

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 ,
Mar 31, 2020 Mar 31, 2020

Copy link to clipboard

Copied

LATEST

Sorry, yours does appear... Did you reply to the main question? That should still work.

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