Time calculation based on text strings

I have a timesheet form that users enter data in military format without the colon sign and I would like to be able to calculate the difference between stop time and start time in a decimal hours format. Example:
Column A Column B Column C
StartTime StopTime HoursWorked

StartTime and StopTime are entered as a string without colon, such as 0600 or 1315. I would like the HoursWorked calcuation to be show as decimal format: 7.25 for 1315-0600. I'm fairly well versed in Excel, but need the digital signature capabilities in Acrobat for the time sheets and I have very limited experience with javascript. Any assistance would be greatly appreciated.


Marshall Anderson


3 Answers

Hi

You might want to have a look at some of the great tutorial files on the learning centre

http://acrobatusers.com/tutorials/wor...


Sean Mitchell   

The time tutorials are based on using the default formats provided by Adobe and they do not include the Military 24 hour format. Also the Adobe format properly considers the valid times for a 24 hour period from midnight, 0000, to the time unit before midnight, 2359.99.9999, or there is no 2400 for the end of the day.

It should be possible to write the custom keystroke, format, and validation scripts for Military time to include 2400 for the end of the day.

Also the Acrobat and JavaScript Date Time object expect a date value along with the time value. So if you want to compute the time within a day and not include a date value the following script could be use for hours and minutes in the 12 hour, civilian 24 hour and military 24 hour formats:

function Time2Num(cTime){
// convert time string h:MM tt or HH:MM to minutes
/* this code can also be used to add multiple
time interval values like in a time sheet */
var min = 0
if ( /(\d{0,2})[:](\d{2})[ ]?([ap])/i.test(cTime) ) {
// civilian time
if(RegExp.$1 != 12)
min = 60 * RegExp.$1; // hour not 12
min += Number(RegExp.$2);
if (RegExp.$3 == "p")
min += 12 * 60; // adjust for time after 11:59.999 am
}
else if (/(\d{0,2})[:](\d{2})/.test(cTime) ||/(\d{0,2})(\d{2})/.test(cTime)) {
// military time
min = 60 * Number(RegExp.$1);
min += Number(RegExp.$2);
}
return min;
} // end function Time2Num

You can then perform the computation of a time interval in minutes and accumulate the weekly or other period values in minutes.

Then you can use a custom format for script to display the time in military 24 hour format:

function Min2Mil(nMinutes) {
// compute the whole hours from the minutes
var nHrs = Math.floor(nMinutes / 60);
// compute the remaining minutes
var nMins = nMinutes % 60;
// format the string
return util.printf("%,2 1.0f" + "%,202.0f", nHrs, nMins);
} // end function Min2Mil

or in hours and minutes:

function Min2hMM(nMinutes) {
// compute the whole hours from the minutes
var nHrs = Math.floor(nMinutes / 60);
// compute the remaining minutes
var nMins = nMinutes % 60;
// format the string
return util.printf("%,2 1.0f"+ ":" + "%,202.0f", nHrs, nMins);
} // end function Min2hMM




George Kaiser   

Try the following script:

function Time2Num(cTime){

// convert time string h:MM tt or HH:MM to minutes;

/* this code can also be used to add multiple time interval values like in a timesheet */;

var min = 0;

if ( /(\d{0,2}):[ ]?([ap])/i.test(cTime) ) {

// civilian time;

if(RegExp.$1 != 12)

  min = 60 * RegExp.$1; // hour not 12; 

min += Number(RegExp.$2);

if (RegExp.$3 == "p")

  min += 12 * 60;  // adjust for time after 11:59.999 am; 

}

else if (/(\d{0,2}):/.test(cTime) ||/(\d{0,2})(\d{2})/.test(cTime)) {

    // military time;

    min = 60 * Number(RegExp.$1);

    min += Number(RegExp.$2);

    }

return min;

} // end funciton Time2Num;

var StartTime = "0600";

var EndTime = "1315";

var ElapsedMin = Time2Num(EndTime) - Time2Num(StartTime);

app.alert(EndTime + " less " + StartTime + " results in an elapsed time of: " + ElapsedMin + " minutes or " + util.printf("%,01.2f", (ElapsedMin / 60)) + " hours", 3, 0);


George Kaiser   


Please specify a reason: