These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to calculate elapsed time and convert to HH:MM:SS format

EvieDee
Registered: Jul 2 2010
Posts: 12
Answered

I am very new to Livecycle and need some assistance with a form that I am trying to create. I have done a lot of Internet searches but nohing so far.

I have a form that I want to calculate elapsed time and then convert to a time format: hour, minute and time.

Here is what I have:

topmostSubform.Page1.ElapsedTime::calculate - (FormCalc, client)
var elapsed = (Time2Num(EndTime.formattedValue, "HH:MM:SS") - Time2Num(StartTime.formattedValue, "HH:MM:SS")) / (1000 * 60)

if (elapsed < 0) then
elapsed = 1440 + elapsed
else
elapsed
endif

// hours
var Hrs = Ltrim(Str(Floor(elapsed / 60)))

// min
var Mins = Right(Concat("0", Ltrim(Str(Mod(elapsed, 60)))), 2)

// sec
var Secs = Right(Concat("0", Ltrim(Str(Mod((elapsed*60),60)))), 2)

//display on form
Concat (Hrs, ":", Mins, ":", Secs)

Problem with this is that if seconds are equal to or less than 30, then the calculation is off by a minute.

For example, if starttime is 12:10:20 and endtime is 13:00:00, instead of getting elapsedtime = 00:49:40 I get 00:50:40.

Thanks in advance and any help will be greatly appreciated!!!

radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hi,

you can use FormCalc for this purpose.

  1. var StartTime = Time2Num(StartTime.formattedValue)
  2. var CurrentTime = Time()
  3. var Difference = CurrentTime - StartTime
  4. // - 7200000 milliseconds because the time epoch starts at 2 o'clock but we need 0 o'clock
  5. $ = Num2Time(Difference - 7200000, "HH:MM:SS")

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Note that the offset from UTC or GMT varies by time of year and longitude from the Greenwich Meridian, also the offset form UTC will be eliminated in the calculation.
----- form1.#subform[0].ElapsedTime::calculate: - (FormCalc, client) ------------------------------- if(HasValue(EndTime) and HasValue(StartTime) ) then// compute elapsed time only if there is an end and start timevar elapsed = (Time2Num(EndTime.formattedValue, "HH:MM:SS") - Time2Num(StartTime.formattedValue,  "HH:MM:SS")) / (1000 * 60)// hoursvar Hrs =  Ltrim(Str(Floor(elapsed / 60))) // minvar Mins = Right(Concat("0", Ltrim(Str(Mod(elapsed, 60)))), 2) // secvar Secs = Right(Concat("0", Ltrim(Str(Mod((elapsed*60),60)))), 2) //display on formConcat (Hrs, ":", Mins, ":", Secs)else// if either is missing do not compute"00:00:00"endif

George Kaiser

EvieDee
Registered: Jul 2 2010
Posts: 12
Radzmar,

Thanks for responding to my post…your time is greatly appreciated.
Your suggestion did not actually help because you may have misunderstood my point. I am not trying to subtract the StartTime from my computer’s CurrentTime, but rather the start time from the end time, both of which are supplied by the users. What I want is to have a formula in the elapsed field that will convert the difference (Endtime – StartTime) in the form of HH:MM:SS.


Gkaiseril,

Likewise, I appreciate your time helping me with this form. However, I am still having the same problem with the calculation as before. I am off by a minute.

Example:
If the StartTime is 12:12:12 and the Endtime is 13:00:00, then the ElapsedTime should be 12:47:48, however, I am getting 12:48:48. I did notice one slight difference between your script and mine, and that is, if my seconds (:SS) are from 1 to 30 then the elapsedTime is off by 1 min. If the seconds are from 31 to 59 then the ElapsedTime gives me the correct answer.

With your formula, I am always off by 1 minute regardless of what the seconds are.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Then only need to substitute the computer's time for the end time.

George Kaiser

EvieDee
Registered: Jul 2 2010
Posts: 12
gkaiseril wrote:
Then only need to substitute the computer's time for the end time.
Gkaiseril,

Not sure what you mean by this. I do NOT want to use the computer's time (time()). I just want to calculate any time that is used
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
You only need to replace the variable CurrentTime with a new variable that is defined in the same way as the variable StartTime.

  1. var StartTime = Time2Num(StartTimeField.formattedValue)
  2. var EndTime = Time2Num(EndTimeField.formattedValue)
  3. var Difference = EndTime - StartTime
  4. // - 7200000 milliseconds because the time epoch starts at 2 o'clock but we need 0 o'clock
  5. $ = Num2Time(Difference - 7200000, "HH:MM:SS")

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

EvieDee
Registered: Jul 2 2010
Posts: 12
Again, thanks to both Radzmar and Gkaiseril for your quick responses. I can't stress enough how appreciative I am of the service you guys offer to so many. While both provided useful information, Radzmar's script and explanation actually got me the better results. Gkaiseril's script gave incorrect numbers for the seconds although the hour and minute were correct.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The code I have used is:
// clear field"00:00:00"// calculate if we have dataif ( HasValue(StartTime) and HasValue(EndTime) ) then// get start time in milliseconds and convert to secondsvar StartSec = Floor( Time2Num(StartTime.formattedValue, "HH:MM:SS") / 1000)// get start time in secondsvar EndSec = Floor(Time2Num(EndTime.formattedValue, "HH:MM:SS") /1000)// compute difference in secondsvar DiffSec = EndSec - StartSec// compute and format whole seconds onlyvar Sec = Right( Concat("0",  Mod(DiffSec, 60) ), 2)// compute and format whole Minutesvar Min = Right( Concat("0", Mod( Floor(DiffSec / 60), 60) ), 2)// compute and format whole hoursvar Hrs = Right( Concat("0", Floor(DiffSec / (60 * 60) ) ), 2)Concat(Hrs, ":",Min, ":", Sec)endif

The above code will also deal with elapsed times that exceed 24 hours.


I tried to use your original code. But it had a number of errors like covertering milliseconds to minutes by dividing by 1000 * 60. You also used the 'Ltrim' function when you should have used the 'Right()' function.

UTC has a different offset in New York, New York. With no data, I see 17:00:00 for the elapsed time. Also the time zone offset will change to one hour at the end of October.

George Kaiser

EvieDee
Registered: Jul 2 2010
Posts: 12
Gkaiseril,

Thanks again for dedicating so much of your time to helping me on this issue. I really like the way you organize your script especially the way you use your comments. Even a novice like myself was able to follow what's going on in the script.

I had one problem with the calculation...if the StartTime is 23:00:00 and the EndTime is 01:00:00, I get 22 hrs for the ElapsedTime instead 2 hrs. I was also getting negative time when the StartTime is bigger than the ElapsedTime, but I was able to fix this using an IF statement.

Anyway, thanks again and I look forward to your expert advice my latest post about "finding the average of multiple fields that contain zero and null values."
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you want to work over a change in date, you also need to include the date, otherwise the assumption is that the times are on the same date. It is possible to enter the date and time for the form if you use the proper format for the fields and in the script.

And if the start time is after the end time then you are receiving the elapsed time past the end date, sort of like the count down to a rocket launce and then the T minus time indicainf the time of the flight after the launch event.

George Kaiser