9 Replies Latest reply on Sep 29, 2015 8:45 AM by discrhythmia

# Script for calculating start time and end time

I am using Adobe Acrobat Pro and needing a script to calculate start time and end time for a timesheet (start time and end time are hh:MM) 24 hours please see below can anyone help me ......

• ###### 1. Re: Script for calculating start time and end time

Will all the time events happen within one day?

Will any event happen at midnight of the day?

How do you want the start and end times calculated?

I know how to compute the elapsed time from the start and end times.

Is this for just one row of data, one day, or multiple rows or days?

Do you need the total of all the "Total Hours" rows?

How do you want the computed time difference displayed, as hours and decimal hours or hours and minutes?

• ###### 2. Re: Script for calculating start time and end time

It will be a daily work sheet (events will happen within one day)

The day's hours will start from 08:00 am to 16:30pm

The start and end times will be free text the guys will input them themselves but in 24 hour format it's the total hours I need a script for

This will be multiple rows (they complete more than 1 job a day)

I do need the total to populate in the "Total Hours" column for each individual row

Time to be shown as hours and decimal hours

Thanks

• ###### 3. Re: Script for calculating start time and end time

First, if you are using 24 hour civilian time format there is no pre or post meridian time, am or pm, since all time values within the day are unique.

You set the formats for Text72 and Text73 to "Time" "HH:MM" and Text74 to "Number" with 2 decimal places.

Because certain calculations will be repeated for many rows, I would use functions to define the calculation code sequence once and then just call that function with the necessary parameters to provide the result of the calculation. The functions that are needed are to convert the time strings to minutes, compute the difference in minutes for the time in and time out with a given format, and rounding the difference to 2 decimal places.

// document level functions

function Round(nValue, nPrecision) {
// round a number a given precision
return util.printf("%,100." + nPrecision + "f", nValue);
} // end round

function Time2Min(cFormat, sTime) {
sTime = sTime.toLowerCase();

if( (cFormat == "h:MM tt" | cFormat == "hh:MM tt") & ( (sTime.substr(0, 2) == "12" & sTime.substr(6,1) == 'a') | (sTime.substr(0, 2) == "12" & sTime.substr(5,1) == 'a') ) )
sTime = "00" + sTime.substr(2, sTime.length - 2);
// convert a formatted time string to number of minutes since midnight
// get time object
var oTime = util.scand("mm/dd/yyyy " + cFormat, "01/01/1970 " + sTime);
// convert to milliseconds
var nTime = oTime.getTime();
// convert to milliseconds to minutes and return value
nTime = nTime / (1000 * 60);
// return round to whole minutes
return Round(nTime, 0);
} // end Time2Min

function TimeDiff(cFormat, cTimeIn, cTimeOut) {
var nDiff = 0;
// get the value of the time in and out fields
var sOut = this.getField(cTimeOut).value;
var sIn = this.getField(cTimeIn).value;
// compute if we have both values
if(sOut != "" && sIn != "") {
// convert time string to minutes and compute difference
nDiff = Time2Min(cFormat, sOut) - Time2Min(cFormat, sIn);
// convert to hours
nDiff = nDiff / 60;
}   // end if sOut & sIn
// return rounded value
return Number(Round(nDiff, 2));
}

// end of document level functions

You can then use a custom calculation where you only need to change the names of the time in and time out fields for the Total Hours field. For the time in field of "Text72" and the time out field of "Text73" the computation for the Total Hours field "Text74" could be:

var cIn = "Text72", cOut = "Text73";

event.value = TimeDiff("HH:MM", cIn, cOut);

// convert zero entry to null

if(event.value == 0) event.value = "";

For the grand total of hours you can use the "Field is the Sum of the following fields:" and select the "Total Hours" fields to sum.

Sample Time Sheet

• ###### 4. Re: Script for calculating start time and end time

Thank you for your response it was very useful however the guys have mentioned to me that they would like the time for the complete day as they will be completing night works on occasions so there time would look like this:

So the negative should read 20:00 to 04:00 total hours 8.5, is there anything in the way of updating the script to show this?

Real appreciative of the help

• ###### 5. Re: Script for calculating start time and end time

If you are covering more than one day, it is best to include the dates. You could assume that if the end time is before the start time, you are ending in the next day and could add 12 hours to the calculation.

The above assumes the shift will be less than 12 hours. If over 12 hours, than you need some way to determine that and having the date would resolve that issue. But then your calculation will use a different approach for the calculation. You would use the JavaScript date object and its various methods to compute the elapsed time.

• ###### 6. Re: Script for calculating start time and end time

What would the script look like to add 12 hours to the calculation?

• ###### 7. Re: Script for calculating start time and end time

Change:

// convert time string to minutes and compute difference

nDiff = Time2Min(cFormat, sOut) - Time2Min(cFormat, sIn);

to

// convert time strings to minutes:

var nOut = Time2Min(cFormat, sOut);

var nIn = Time2Min(cFormat, sIn);

if(nOut < nin) nOut =+= 1440; add 24 hours for assumed next day;

// convert time string to minutes and compute difference

nDiff = nOut - msIn);

• ###### 8. Re: Script for calculating start time and end time

Hi,

I changed the script as follows however an error message came up?  All i did was take the thread and paste over what you told me to change

• ###### 9. Re: Script for calculating start time and end time

I'm running into the same problem as when he change the script to correct for an end-time that occurs the following day.  Would you mind reviewing this thread and helping solve the syntax error?

Thanks