Calculating Elapsed time in Seconds
I've seen this question asked several different ways and everyone has a different way of doing it which is great but has made it difficult for me to apply it to my form.
I have several instances where a start and stop time are entered via a button in the HH:MM:ss tt format. I need to be able to calculate elapsed time in hours, minutes or seconds. The easiest way I figure is to standardize to seconds and have field calculations convert to whichever I need in that section.
I found a document level script on the acrobatusers.com page that works great for converting to minutes but doesn't take into account seconds. So a difference of 5 minutes and 10 seconds would only show as 5 minutes.
// document level functions for reuse
function Time2MinRE(cTime){
// convert time string h:MM tt or HH:MM to minutes
var nMin = 0
if ( /(\d{0,2})[:](\d{2})[ ]?([ap])/i.test(cTime) ) {
// civilian time
if(RegExp.$1 != 12)
nMin = 60 * RegExp.$1; // hour not 12
nMin += Number(RegExp.$2); if (RegExp.$3 == "p")
nMin += 12 * 60; // adjust for time after 11:59.999 am
}
else if (/(\d{0,2})[:](\d{2})/.test(cTime)) {
// military time
nMin = 60 * Number(RegExp.$1);
nMin += Number(RegExp.$2);
}
return nMin;
} // end function Time2MinRE
// end document level function
I've been able to edit the script above to display elapsed time in seconds but it still does not take the actual elapsed seconds into account. So an elapsed time of 5 minutes and 10 seconds would show as 300 not 310.
I believe I'm missing something I need to change in the first if statement for civilian and military times. I added an additional [:](\d{2}) to the statements in hopes that I could call it with a RegExp.$3 statement and it would pick up the seconds but it doesn't seem to work that way an I haven't been able to find guidance on what this line is actually saying. I just recognize its pointing towards parts of the time object. Any guidance would be appreciated.
// document level functions for reuse function
Time2SecRE(cTime){ // convert time string h:MM:ss tt or HH:MM:ss to Seconds
var nSec = 0
if ( /(\d{0,2})[:](\d{2})[:](\d{2})[ ]?([ap])/i.test(cTime) ) {
// civilian time
if(RegExp.$1 !=12 )
nSec = 3600 * RegExp.$1; // hour not 12
nSec += 60*Number(RegExp.$2);
nSec+=Number(RegExp.$3);
if (RegExp.$4 == "p")
nSec += 12 * 3600; // adjust for time after 11:59.999 am
}
else if (/(\d{0,2})[:](\d{2})[:](\d{2})/.test(cTime)) {
// military time
nSec = 3600 * Number(RegExp.$1);
nSec += 60*Number(RegExp.$2);
nSec+=Number(RegExp.$3);
}
return nSec;
} // end function Time2SecRE
// end document level function
