Print Page | Close Window

Caclulating Overtime Hours

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: [Commercial] Foxit Reader / Foxit Phantom
Forum Description: View, Create, Annotate, and Print PDF Files with Foxit Reader
Printed Date: 29 Feb 2020 at 12:43pm
Software Version: Web Wiz Forums 11.10 -

Topic: Caclulating Overtime Hours
Posted By: Andy63
Subject: Caclulating Overtime Hours
Date Posted: 10 Jul 2017 at 3:49am
Hello Everyone,

I'm in the process of designing a timesheet, and need to work out how to get Foxit PDF to automatically calculate and add the total no. of hours worked on a given date.
I've attached a PDF file with an explanation of what I am looking for, along with the fields that I am working with,

I've been to several JS sites to look at possible solutions, but so far I've run into 'syntax error' issues whenever I have tried to import them.

Help appreciated

uploads/3314/Time_Trial.pdf - uploads/3314/Time_Trial.pdf

Posted By: gkaiseril
Date Posted: 10 Jul 2017 at 7:51pm
JavaScript has some standardization but there is no one single version of JavaScript that will work in every possible environment. The JavaScript used for web or OS applications has very different features and syntax than what is used with Acrobat and PDFs.

Date and time calculations use the system clock and the very smallest unit of time measurement is the millisecond. Also the JavaScript date object adjust for Daylight Savings Time or Summer time changes automatically. Time without a date reference is not an accurate way to compute time. As noted there can be an issue with DST or ST and if one is measuring a time interval that crosses a day boundary needs to account for this change in dates. Once can make an assumption that the date is not important and the DST or ST time shifts can be ignored and if there is no change in the use of time sheet with these restrictions all should be all right. But if the time sheet needs to cover the time period in which the there is a time change or shifts cover more than one day then the time sheet will fail.

I have found that computing and recording the values of the time worked is best done in minutes and then using a custom format to convert computed minutes to the H:MM format. This will make the weekly total easier to compute.

For forms that have repetitive rows I would use the hierarchical naming convention for the field names. This will allow one to compute the row number from the field in which there is a calculation based on the target field's name.

So using to specific date, your custom calculation for each "Line Total" could be:

// compute number of row for calculation;
var cFieldName =; // get focused field's name;
var aFieldName = cFieldName.split("."); // split by level maker;
var nRow = aFieldName[aFieldName.length -1]; // get row number;
// get values from the input fields;
var cStart = this.getField("Start Time." + nRow).value;
var cEnd = this.getField("Finish Time." + nRow).value;
var cBreak = this.getField("Lunch." + nRow).value;
// convert input values to minutes;
// start time;
var oStart = util.scand("d-mmm-yyyy h:MM tt", "1-Jan-1970 " + cStart); // convert to date object;
var nStart = Math.round(oStart.getTime() / (1000 * 60)); // convert minutes;
// end time;
var oEnd = util.scand("d-mmm-yyyy h:MM tt", "1-Jan-1970 " + cEnd);
var nEnd = Math.round(oEnd.getTime() / (1000 * 60));
// compute difference between end and start;
var nDiff = nEnd - nStart;
// break time
var aBreak = cBreak.split(":"); // split break time into array of hours and minutes;
var nBreak = (aBreak[0] * 60) + aBreak[1]; // convert elelments of array into minutes;
// subtact break time;
event.value = nDiff - nBreak;

The "Final Total" is just the sum of each line total.

The custom format for all the calculated fields is:

event.value = util.printf("%,101.0f:%,102.0f", event.value / 60, event.value % 60);

Posted By: Andy63
Date Posted: 14 Jul 2017 at 2:58am
Hi gkaiseril,

OK, in this case the actual date is filled in manually for each line, so I'm not sure if it is that important. I only need to calculate the no. of hours worked, minus any time taken for lunch. 

I ran the "Line Total" script through the JS console, and got the following error message:
[ Line: 00002 { TypeError } ] : Cannot read property 'split' of undefined

I also ran the custom format field through the JS console, and got this error message:
[ Line: 00000 { event.value } ] : (null)

I am a complete ignoramus when it comes to JS, so I have no doubt I am doing something wrong somewhere. Sorry for trying the patience of people, but I suspect this is a bit of a 'holy grail' for forms like this, so I'm keen to see if it can be sorted.

I have searched around on other PDF forums and tried a couple of different script formats, as well as some (very amateurish) attempts to fix it myself, with no real progress.

Posted By: gkaiseril
Date Posted: 14 Jul 2017 at 2:56pm
You have the have the fields proper name. I used hierarchical field names so I would not have to created 2 different scripts, one for the first row and another for all the other rows. This approach also allows me to create the first row and calculation and then just create multiple copies of the first row and the scripts will self adjust for each row. Most custom JavaScirpts cannot just be cut and pasted without some editing for the situation. - Time Trial

Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 -
Copyright ©2001-2017 Web Wiz Ltd. -