Planet PDF Forum Planet PDF Forum
  New Posts New Posts RSS Feed - Caclulating Overtime Hours
  FAQ FAQ  Forum Search   Register Register  Login Login


Hi, welcome to the Foxit Planet PDF Forum. If you have PDF or Adobe Acrobat questions then the right place to ask them is here, in this forum.

Caclulating Overtime Hours

 Post Reply Post Reply
Author
Andy63 View Drop Down
New Member
New Member
Avatar

Joined: 27 Jun 2017
Points: 8
Post Options Post Options   Quote Andy63 Quote  Post ReplyReply Direct Link To This Post Topic: Caclulating Overtime Hours
    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

Back to Top
Sponsored Links


Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4071
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post 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 = event.target.name; // 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);

Back to Top
Andy63 View Drop Down
New Member
New Member
Avatar

Joined: 27 Jun 2017
Points: 8
Post Options Post Options   Quote Andy63 Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4071
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

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

This page was generated in 0.030 seconds.