Timesheet - Calculate the hours

Hi

I am trying to make a timesheet for the guys that will calculate automatically rather than them needing to.

Basically I have 3 columns (mulitple rows): StartTime, FinishTime, TotalHours

I have set the first two columns to format the entry as as a 24hr clock but a simple field notation calculation of those two columns does work as it does the (deduction) maths as a number not as hours eg StartTime 07:00 - FinishTime 15:30 returns 8.30 instead of 8.50 (8 and a half hours).

Because I then have a final cell that will have to add all the above TotalHours as Hours.

I understand that it will be javascript but I simply have no idea.

Help will be appreciated!

David


David Rew


3 Answers

Voted Best Answer

This issue was discussed many times on these forums. Try searching around and you'll find explanations and code samples.

If you're interested in a pre-made script that will help you set it all up easily and without needing to write any code yourself, though, check out this one (developed by me): Acrobat -- Calculate Time Differences in a Worksheet

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


By Gilad D (try67)   

Your question is not completely clear to me: do you need difference as hours or as a number?

The following script is making it as a number (0-100).

Leave your StartTime and FinishTime as they are (Time format HH.MM that means HH:MM within Acrobat JavaScript Engine).
In TotalHours | Calculate | Custom Calculation 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;

You may consider dividing by 60 instead of 59.
Hope it helps.


Ricardo Falegnami   

Simplified field notation cannot perform date or time arithmetic. The routines behind the user interface is assuming the ':" is decimal place and the values are numbers in the base 10 numbering system and not valued in the date time cyclical system.

Thom Parker has written a series of tutorials in the Learning Center presenting one method for computing elapsed dates and times.

Adobe has free JavaScripting References online and has the Date arithmetic example using the milliseconds from the Epoch date of JavaScript.

It is also possible to split the time string into hours and minutes and then convert those 2 values a common value like minutes or hours and then finish your computations using the standard mathematical operands. If you keep your intermediate step fields as decimal values either hours or minutes the totaling becomes much easier and you can use custom formatting to change the displayed value.

Note that formatting for the input data and processing is very important. JavaScript does make assumptions about dates and times based on the format of the field and this might not be the value that JavaScript sees. For example, with the 24 hour format one could enter 12:00am, 00:00 in 24 hour format, for the start of the day but any script accessing that field's value will see 12:00 or 12:00pm Noon and not 00:00.


George Kaiser   


Please specify a reason: