Calculating Elapsed time in Seconds

New Here ,
Nov 29, 2018 Nov 29, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

213

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Nov 29, 2018 Nov 29, 2018
So I didn't look at your regular expressions before. They are incorrect. This is the sort of thing that needs to be tested first, before writing a function around it.  Always use the console window to test these kinds of small bits before using them. The ":" is a special regular expression character, so it has to be escaped. It also doesn't make sense to use it in the square bracket grouping since there is only one.  Try this:/(\d{0,2})\:(\d{2})\:(\d{2})/

Likes

translate

Translate

Translate
Adobe Community Professional ,
Nov 29, 2018 Nov 29, 2018

Copy link to clipboard

Copied

What you are missing is the modulus operator, which returns the remainder of a division.  So if you have the total number of seconds, then you can determine the number hours, minutes and seconds like this.

var nTotalSecs = ...;

var nHours = Math.floor(nTotalSeconds/3600);

var nSecLeft = nTotalSectons%3600;

var nMinutes = Math.floor(nSecLeft/60);

var nSeconds = nSecLeft%60;

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 29, 2018 Nov 29, 2018

Copy link to clipboard

Copied

Thanks Thom.

Am I correct in understanding that your suggestion assumes I have the correct  elapsed seconds already?  The problem I'm having is that the function I'm using isn't giving me the total seconds of elapsed time. In its current form its only converting total elapsed minutes to seconds and ignoring the elapsed seconds between start and stop. I don't see that its taking those remainder seconds into account. Where would I put your suggested code?

After re-reading my post and title I think its deceiving. I guess the real problem I'm having is that I'm having trouble editing this function that converts time of day in hours and minutes (HH:MM tt) to minutes (MM) to a function that converts hours, minutes and seconds (HH:MM:ss tt) to seconds (ss). The calculation for elapsed time happens in a separate field by taking the start and end timestamps, running them through this function and subtracting them to get elapsed time.

I'm talking out loud to help me understand and hopefully someone can correct where I'm wrong:

Time2SecRE() pulls the time from a field (either start time or end time) and is supposed to convert that to seconds (10:01:00 am would be 36,060 seconds).

In the function it looks like the RegExp.$# points to expressions in the if statement if (/(\d{0,2}) [:] (\d{2}) [:] (\d{2}) [ ]?([ap)/i.test(cTime)){

The function sees the first expression as hours and the second expression as minutes so I added a third expression [:] (\d{2}) thinking it would pull the seconds from the time stamp but it doesn't seem to be doing that. so 10:01:10 still shows as 36,060 seconds and not 36,070 seconds.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 29, 2018 Nov 29, 2018

Copy link to clipboard

Copied

So I didn't look at your regular expressions before. They are incorrect. This is the sort of thing that needs to be tested first, before writing a function around it.  Always use the console window to test these kinds of small bits before using them.

The ":" is a special regular expression character, so it has to be escaped. It also doesn't make sense to use it in the square bracket grouping since there is only one.  Try this:

/(\d{0,2})\:(\d{2})\:(\d{2})/

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 30, 2018 Nov 30, 2018

Copy link to clipboard

Copied

Thanks Thom, that did the trick!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines