Skip to main content
tacnola
Known Participant
March 17, 2020
Answered

Need Help Fixing the Code I am Using to Calculate Time

  • March 17, 2020
  • 3 replies
  • 2989 views

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!!

This topic has been closed for replies.
Correct answer try67

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

3 replies

try67
Community Expert
March 31, 2020

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

try67
Community Expert
March 31, 2020

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

Brainiac
March 31, 2020

Curiously, I do see my reply.

Brainiac
March 31, 2020

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

try67
Community Expert
March 17, 2020

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

tacnola
tacnolaAuthor
Known Participant
March 27, 2020

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

try67
try67Correct answer
Community Expert
March 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;}