Copy link to clipboard
Copied
Hi Guys,
I wanted to know, Is it possible to create a Javascript Function based calculation for a timesheet that also includes amount of people working during that time period.
Example.
Start | Finish | Number of workers | Total Hours |
---|---|---|---|
Start1.0 | Finish1.0 | Workers1.0 | Totalhours1.0 |
Start1.1 | Finish1.1 | Workers1.1 | Totalhours1.1 |
Start1.2 | Finish1.1 | Workers1.2 | Totalhours1.2 |
My function would be something like CalcHours() ….. which would yield something like would calculate like this (Finish1.0 - Start1.0) * Workers1.0
your helps would be greatly appreciated.
Copy link to clipboard
Copied
Is there a particular reason to use a Javascript function for this simple calculation? Otherwise just use a Simplified Notation calculation field for your total hours field with this formula:
((finishTime - startTime)*numberOfWorkers)
Copy link to clipboard
Copied
The reason is it won't work that way... Time calculations require using a script.
Copy link to clipboard
Copied
TRy67, do you think you would know how to solve this? you seem like you know your stuff.
Many people would appreciate it.
Copy link to clipboard
Copied
I've developed a (paid-for) tool that allows you to set up such calculations very easily. You can find it here: Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet
Once you've calculated the difference between the two time fields multiplying it by the number of workers is trivial.
If you're interested I could set it all up for you (for a fee). You can contact me privately (try6767 at gmail.com) to discuss it further.
Copy link to clipboard
Copied
try67 wrote
The reason is it won't work that way... Time calculations require using a script.
Oops, that's true. Been a while since I worked with PDF forms.
How about this hacked together script:
// document functions
function HHMM2Min(cTime) {
var aHHMM = cTime.split(":");
var nMinutes = 0;
nMinutes = aHHMM[0] * 60;
nMinutes += Number(aHHMM[1]);
return nMinutes;
}
function Min2HHMM(nMinutes) {
return util.printf("%,001.0f:%,202.0f", Math.floor(nMinutes / 60), nMinutes % 60);
}
function calcHours(startTime,endTime,numberOfWorkers) {
var hrsStart = parseInt(startTime.split(":")[0]);
var minStart = parseInt(startTime.split(":")[1]);
var hrsEnd = parseInt(endTime.split(":")[0]);
var minEnd = parseInt(endTime.split(":")[1]);
if (minStart > minEnd) {
var minRez = 60 + minEnd - minStart;
var hrsRez = hrsEnd - 1 - hrsStart;
}
else
{
var minRez = minEnd - minStart;
var hrsRez = hrsEnd - hrsStart;
}
timeTotal = hrsRez + ":" + minRez;
sTime = timeTotal;
nSum = HHMM2Min(sTime) * numberOfWorkers;
totalHours = Min2HHMM(nSum);
return totalHours;
}
//calc field
timeStarted = this.getField('timeIn').value;
timeFinished = this.getField('timeOut').value;
workerNumber = this.getField('multiplier').value;
event.value = calcHours(timeStarted, timeFinished, workerNumber);
It does what you want, I think. I only tested with one timeIn time input field, one timeOut input field, and one multiplier (workers) field.
Copy link to clipboard
Copied
Your script will not work correctly if the time interval you are trying to compute includes the change in Daylight Savings time. If you time sheet will include that time, then you must include the date since the time change occurs on very specific days. JavaScript will know the dates and time based on the timezone setting of your computer.
Copy link to clipboard
Copied
Yeah, I realized that this morning as well while looking at it with fresh eyes. Initially I assumed the OP wanted a simple hours a day worked, but re-reading the OP's question, he mentions a 'time period' which probably should include a full date & time object.
Back to the drawing board, if I can find the time. Or it might resolve itself if we wait long enough for all countries to abolish daylight savings