Copy link to clipboard
Copied
Hi
Im looking for a simple java script that will calculate the time in decimal between start / finish time for one week day and also minus a half hour for breaks. The script does not need to account for calendar days as the time frames will always be within the same day.
Ive tried several scripts and none seem to work for me. (exp 09:40 to 12:30 = 3.16 and not 2.83 on one script ) The field names i am using are "StartTime" and "FinishTime" The time is 24hr format and I have set the format on the text field to HH:MM.
When i have added all the weekdays up in decimal.I would like to convert the decimal back to HH:MM. Appreciate any help
Copy link to clipboard
Copied
What script does you use?
Copy link to clipboard
Copied
Hi . My apologise I dont have the script to hand as it was one of four that I tried from those published in this community . If think that it may have been this script
// start var start = this.getField("StartTime").value; var startArr = start.split(":"); // finish var finish = this.getField("FinishTime").value; var finishArr = finish.split(":"); // difference var hourDiff = Math.abs(finishArr[0] - startArr[0]); var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 59)*100); if (minDiff.toString().length == 1) minDiff = '0' + minDiff; var output = hourDiff + "." + minDiff; event.value = output;
Copy link to clipboard
Copied
The problem is with this line of the code:
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 59)*100);
I don't know why they used 59 instead of 60, for starters. Also, using the absolute value of the difference between the two minutes is incorrect. If you do that you can't know if the result is positive (because the end time's minutes are larger than the start time's minutes) or negative (if they are smaller).
Copy link to clipboard
Copied
I did try dividing by 60 because it was suggested to do so in the original post . Can you explain the discrepancy between the absolute value and I will apply . Hopefully this will work . Once the script calculates correctly. I need to minus a half hour from the result for lunch break . Do I just insert - 0.5?
Copy link to clipboard
Copied
E.g. 09:40 to 12:30 will give 10 minutes as result. This is wrong.
Copy link to clipboard
Copied
I explained it above. It's incorrect because it treats the difference between 9:30 and 10:40 as the same as between 9:30 and 10:20... I've developed a (paid-for) tool that allows you to set up such calculations easily (and correctly), without having to write any code. You can find it here: http://try67.blogspot.com/2011/03/acrobat-calculate-time-differences-in.html
And if you get it I could help you set up the deduction of the 0.5 hours for lunch, that's not a problem.
Copy link to clipboard
Copied
Thanks for the reply. I'm not familiar with java, Can you a amend the code ? I dont have the budget to purchase additional software . My aim here was to streamline my work flow and thought that it would be just a simple line of code to take one time from another and deduct a half hour. This surely cant be that difficult especially when the times are not dependant on dates. Strange that adobe have not introduced a simple function to achieve this . I appreciate any help.
Copy link to clipboard
Copied
Hi. This is the latest script that i have tried to minus one time from another in 24hr format
// start
var start = this.getField("StartTime").value;
var startArr = start.split(":");
// finish
var finish = this.getField("FinishTime").value;
var finishArr = finish.split(":");
// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
if (minDiff<0) {hourDiff--; minDiff*=-1;}if (minDiff<0) {hourDiff--; minDiff*=-1;}
var output = hourDiff + "." + minDiff;
event.value = output;
It works fine if i work to the nearest half hour but if i apply the following times 09:45 and 17:30 the script pumps out 7:25 where it should be 7.75. Where am i going wrong here or is it even possible to achieve the correct result so i can conclude my search . As i said before i have no experience of java scripting. Im only trying to adapt a time sheet as pdf''s work well with a software I recently purchased. All I require is to minus one time from another and then subtract a half hour. I would appreciate any help on this.
Copy link to clipboard
Copied
Make sure your field has the Format set to None. It it probably set as a Time field, which is not good in this case.
Copy link to clipboard
Copied
Thanks but that doesnt seem to work for me.Ive removed the format on StarTime and FinishTime and the Total field and tried different combinations. Is there anyway i could lock the fields down to the nearest half hour to stop somebody from adding in 715 or 745
Copy link to clipboard
Copied
Why did you duplicate this line?
if (minDiff<0) {hourDiff--; minDiff*=-1;}
Yes, you can do that with a validation script, or by using a drop-down field with all the possible options, instead of an open text field.
Copy link to clipboard
Copied
Because when i was researching scripts i found a thread which sugested replacing a paticular piece of code but did not say to delete the existing . I did not want to delete a line especially when i have no idea what implications it may have. Besides i have now deleted it and it has made no difference.
Copy link to clipboard
Copied
Use this code:
// start
var start = this.getField("StartTime").value;
// finish
var finish = this.getField("FinishTime").value;
if (start=="" || finish=="") event.value = "";
else {
var startArr = start.split(":");
var finishArr = finish.split(":");
// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = (finishArr[1] - startArr[1]) / 60;
if (minDiff<0) {hourDiff--; minDiff+=1;}
event.value = (hourDiff + minDiff).toFixed(2);
}
Copy link to clipboard
Copied
Thank you that worked. You sir are an absolute gent