Simple Time Calculation for PDF
This is not a question, but a post for being able to do simple time calculation for time sheets that I have come up with that works on both PCs and iOS that does not get into the complicated time functions of Java. This way uses the drop down menus and the export value ability when you add items to that menu. First you will want to create 7 drop down menus in your PDF. Name three of them for StartTime (StartTimeHour or STH; StartTimeMin or STM; StartTimeAP; or STAP), three for EndTime (EndTimeHour or ETH; EndTimeMin or ETM; EndTimeAP; or ETAP), and one Break. If you want you can add 1 to these or just remember to add the number to each new one you start for each line. Now in the drop down boxes for the hours put 1 in the "Item" box and 1 in the "Export Value" box right below then add to the list, do this each for each hour 2-11 with the export value corresponding to the item. NOTE: for hour 12 make sure export value is 0!! This is key for the Java script later. Before closing make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" is unchecked. Next do the drop down boxes for the minutes, we round to the nearest 15 minutes, so "Item" box :15 has "Export Value" box of 0.25; :30 is 0.5; :45 is 0.75; :00 is 0. Make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Next do the drop down boxes "StartTimeAP" and "EndTimeAP" as this will be your AM, export value 0, and PM, export value 12. Again make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Finally in the "Break" drop down menu we have the option from None to 2 hours in 15 minute blocks e.g. None is 0, 0:30 is 0.5, 1:15 is 1.25, etc. Again make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Now with those done create a Text Field box and name it TotalWorked, no need to shorten as it will not be referenced in the Java. Now in the Properties of the "TotalWorked" box go to the Calculate tab and choose "Custom calculation script" and click the "Edit" button. Here you will copy in the following Java script:
//Get Time Values
var StartTimeHour = this.getField("STH1").value;
var StartTimeMin = this.getField("STM1").value;
var StartTimeAP = this.getField("STAP1").value;
var EndTimeHour = this.getField("ETH1").value;
var EndTimeMin = this.getField("ETM1").value;
var EndTimeAP = this.getField("ETAP1").value;
var BreakTime = this.getField("Break1").value;
//Combine Time Values
var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);
var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);
//Calculate Time
var TimeWorked = ((EndTime - StartTime) - BreakTime);
//Test for Midnight Crossover
if(TimeWorked < 0) {// Shift 24 hours
TimeWorked += 24;
} else if(TimeWorked === 0) {//Shift 24 hours for same times
TimeWorked += 24;
}
//Print Results
event.value = TimeWorked;
And that's it. When you do the preview you will see that all the time calculations come out correct for the total number of hours worked. Again this has been confirmed to work with no issues on PC and iOS, i would assume Android and OSX as well since there is no complicated Java scripting but I have not confirmed since we do no use either of those OS. Also, if you don't round by the nearest 15 minutes you could calculate out what you do round to based on the hour, e.g. 10 min would be 0.1667 hours. However, as you can see this causes repeating numbers and you would have to have to add a round up function into the Java script to compensate, or if not too picky could choose the two decimal option for the TimeWorked box and let the PDF do the rounding. Finally, I have an even simpler way of doing time base on just a StartTime and EndTime drop down. However, this generates very larger drop down boxes and can be a bit difficult to manage on smaller screens. If anyone is interested I can also post that on here as well. I hope this helps out a lot of people since doing time can be so difficult to navigate on PDF. Thanks
