Calculation script for time sheet

I have been trying to write a calculation script for a timesheet that can be filled out by out field personnel.we use iPhones with either adobe acrobat or pdf expert. I am not great with Java script so I have searched the Internet looking for similar forms to get ideas from with not a lot of success.
What I need to calculate is time in to time out minus break time. Then if the time is greater than the specified regular time ( for example 8 hrs) 8 hrs will go into the regular time column and the remainder will go into the overtime column. I have included a link to the file I am working on if anyone wishes to have a look.

https://www.dropbox.com/s/0sxdlmfjvax...

Your help is much appreciated
Greg


Greg Nygren


4 Answers

You have two issues here: One is the writing the actual code that does this (which is not a simple task by itself), and two is making sure that code actually work on iOS devices, which is not certain. By the way, there isn't an Acrobat version for the iOS, only Reader.

I have developed a tool that can be used (in Acrobat) to do the first part (Acrobat -- Calculate Time Differences in a Worksheet), and it can be adjusted to split the results into two parts, but the second issue is just a question of trial and-error, I'm afraid.

.


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


Gilad D (try67)   

I managed to get the first part working using a couple document level scripts that i found. Time2num and TimeDiff this part is working fine on my PC but on IOS it work only up to 12 hours, over that i get strange numbers.


Here is the script that calculates the RegHours form field

event.value = '';
var fDiff = 0;
/*
// compute the difference for first pair of fields
// get the start time
var sStart = this.getField('StartTime1').value;
// get the end time
var sEnd = this.getField('StopTime1').value;
// complete script only if we have data
if(sStart != '' & sEnd != '') {
// convert sStart string to seconds
var fStart = Time2Num('hh:mm', sStart);
// convert sEnd string to seconds
var fEnd = Time2Num('hh:mm', sEnd);
// compute difference in seconds
fDiff += fEnd - fStart;
}
*/

fDiff = TimeDiff('StartTime1', 'StopTime1');


// convert to rounded minutes if not zero
if (fDiff != 0) {
fDiff = Math.round(fDiff / 60);
// report decimal hours
event.value = fDiff / 60;
}


Here is TimeDiff the document level script

function TimeDiff(cStartField, cEndField) {
var sTimeFormat = 'hh:mm';
var fDiff = 0;
// get the start time
var sStart = this.getField(cStartField).value;
// get the end time
var sEnd = this.getField(cEndField).value;
// complete script only if we have data
if(sStart != '' & sEnd != '') {
// convert sStart string to seconds
var fStart = Time2Num(sTimeFormat, sStart);
// convert sEnd string to seconds
var fEnd = Time2Num(sTimeFormat, sEnd);
// compute difference in seconds
fDiff += fEnd - fStart;
}
return fDiff;
}


Greg Nygren   

Here is the other Document level script Time2Num

im not sure where the problem stems from but for example 1:00 am to 2:00 pm on ios results in 1 hour on my pc it is 13 hours. ios works to 11 hours

function Time2Num(sFormat, sTime) {
/*
convert time string (sTime) with format of sFormat
to the seconds since the start of the day
*/
if(sTime == '') return ''; // exit
// get date time for Epoch date and sTime
var oTime = util.scand('mm/dd/yyyy ' + sFormat, '01/01/1970 ' + sTime);
// convert UTC Offset to milliseonds for adjustment
var fTZOffset = oTime.getTimezoneOffset() * 1000 * 60
// time since the start of the day in millseconds
var fTime = oTime.valueOf() - fTZOffset;
// convert to seconds and return value
return Math.round(fTime / 1000);
}


Greg Nygren   

After further testing on IOS it is apparent that any calculation in the afternoon resolves incorrectly. If I calculate a difference of 1 minute the result is 2,880. Does anyone know a work around for this ? All morning calculations seem to work fine.


Greg Nygren   


Please specify a reason: