PRODOK Engineering


28 January 2007
by Max Wyss

Resently a member of the PlanetPDF forum asked about a way to calculate the time difference between two times indicated in a drop down menu. The times in the drop down menu represent quarter hours (0:00, 0:15, 0:30, 0:45, 1:00, etc.).

(Acrobat) JavaScript provides some extensive time (and date) related functionality in the Date object of the Core. However, using this object is sometimes a bit awkward, and requires some scripting: First, the time strings must be converted to a valid date object. Then, the subtraction is executed. And finally, the result is converted to the desired output string.

Sounds complicated…

But there is a much simpler way.

Because we implement a drop down menu either as a Combobox or a Listbox, Acrobat JavaScript provides a very handy field property:

currentValueIndices

This property returns the indices of the selected items. In a Listbox, where we can have multiple selections, it will be an array, but in a Combobox, or a Listbox with a single selection, it will be a simple number. The topmost element in the list bears the number 0, the next one 1, etc.

Because we assume that both, the list for the “from” time and the list for the “until” time are identical, and the intervals in the lists are identical as well, we can calculate with index numbers. In our example, we essentially count quarter hours. And to get the duration in hours, we simply have to divide the resulting number by 4.

The example file (PDF, 26kB) contains two Comboboxes with all times ranging from 0:00 to 24:00 in 15-minute intervals. The “from” list resides in the field from, and the until list resides in the field until. The field duration is a text field, and has an automatic calculation of the difference:

event.value = (this.getField("until").currentValueIndices - this.getField("from").currentValueIndices) / 4 ;

And that’s it. As said, we calculate in “quarter hours”, which means that in order to get hours, we have to divide the result by 4 … and all that in one single line of code.

Because the bare number is a little bit scarce, we embellish the result using a Format event script, which is another one-liner:

event.value = util.printf("%.2f", event.value) + " hours"

This script takes the input value, formats it as a number with a decimal point and two digits after the decimal point, and adds the string " hours" to it. Because we are in the format event, it will only affect the way the field is displayed; the field’s value (to be used for further calculations) is not formatted at all.

And that’s it.

This approach offers a few interesting possibilities. As long as the intervals are equal, any interval could be used, even spanning midnight. It will also allow any way to represent the time, because it does not rely on the displayed text. This takes away the problems with the rather illogical am and pm times (where 12:30am actually means 30 minutes past midnight, and is earlier than 1:15am).

Feel free to use this piece of code, but never forget and always mention where you found it.


AttachmentSize
Simple Time Calculation25.8799999999999990052402 KB