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

Need help with calculating start time and end time and placing the result in a duration text box as minutes not decimal.

Tech264
Registered: Apr 4 2008
Posts: 111
Answered

I did a search on the forum for calculating start time and end time and found the following code which I'm utilizing as a formcalc on the duration field.
==============================================================
form1.Page1.Duration::calculate - (FormCalc, client)
 
if (HasValue(StartTime) and HasValue(EndTime)) then
var in_ = Time2Num(StartTime.formattedValue, "h:MM A")
var out_ = Time2Num(EndTime.formattedValue, "h:MM A")
if (in_ >= out_) then
xfa.host.messageBox("The start time cannot be greater than or equal to the end time.")
$.rawValue = null
else
var diff = out_ - in_
$.rawValue = diff/3600000
endif
else
$.rawValue = null
endif
=============================================================
 
So when I enter a start time for example of 3:30 pm and end time of 4:00
 
The duration field will say 0.5
  
How can I get my form to say 30 minutes instead of placing a decimal number?
 
Secondly, how can I utilize LC patterns field where the employee can just type 330 or 3:30 and it automatically based on the computer's clock put in on the start time 3:30 PM?
 
Any help will be greatly appreciated. Thank you.
    

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Think about what the number "3600000" represents.

There are 1,000 milliseconds/second
There are 60 seconds/minute.
There are 60,000 milliseconds/minute
There are 60 minutes/hour
There are 3,600,000 milliseconds/hour

So 3,600,000 is the number of milliseconds in an hour. Your answer is in hours. So the answer of 0.5 is 1/2 hour. To convert 1/2 hour to minutes you can multiply that value by 60 minutes/hour. Or you could divide the difference in milliseconds by 60,000 milliseconds/minute.

Adding comments and even the steps to compute the number milliseconds for a given time period might help other see and understand the computation.

// test for data for computation
if (HasValue(StartTime) and HasValue(EndTime)) then
// start time in milliseconds
var in_ = Time2Num(StartTime.formattedValue, "h:MM A")
// end time in milliseconds
var out_ = Time2Num(EndTime.formattedValue, "h:MM A")
// test that end is not before start
if (in_ >= out_) then
xfa.host.messageBox("The start time cannot be greater than or equal to the end time.")
$.rawValue = null
else
// compute the difference in milliseconds
var diff = out_ - in_
var Sec = 10000; // milliseconds in one second
var Min = 60 * Sec // milliseconds in one minute
$.rawValue = diff / min
endif
else
$.rawValue = null
endif





George Kaiser

Tech264
Registered: Apr 4 2008
Posts: 111
I've tried your code and I get an error message of Error: accessor 'min' is unknown.

Basically just want the duration field to be able to display "30 minutes" instead of 0.5 with the words minutes included or hour(s).
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Change the line:

$.rawValue = diff / min

to

$.rawValue = diff / Min

George Kaiser

Tech264
Registered: Apr 4 2008
Posts: 111
That error message still appears.

Here's the form so you can see for yourself.

https://acrobat.com/#d=f1kxh5qjuow5ujyZduF8OQ


Tech264
Registered: Apr 4 2008
Posts: 111
Just wanted to revisit this post because I'm still not able to accomplish my goal on this form project. Any help is appreciated.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I see you did not change 'min' to 'Min'. Also need to change the value for Sec to 1000.

You need to divide by the number of milliseconds in a minute and not the number of milliseconds in an hour.

George Kaiser

Tech264
Registered: Apr 4 2008
Posts: 111
It's amazing how a small or capital letter can make a difference in a code. Weird the first time I did it I still had that error message but once I changed the value to Sec to 1000 it disappeared. Thank you Gkaiseril. If you don't mind me asking, how can I add the word minutes next to the resulting answer? So if I enter 2:00pm to 2:30pm instead of just saying 30 which it does now, would like it to say "30 minutes" and if it's more than an hour can a code be entered to say that if it's more than 60 minutes the duration field would say "1 hour" or if more time, "1 Hour, 25 minutes."

Thank you!


gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You asked for the result in minutes.

Further, you are dealing with time intervals that could span 2 days, start before midnight on one day and end after midnight the next day, and it appears that the script that you have will not deal with that situation.

George Kaiser

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

here's a different approach.
Works can calculate the difference in minutes between 2 times, even if they span over two days:

  1. var tStart = Time2Num(StartTime.formattedValue, "HH:MM:SS")
  2. var tEnd = Time2Num(EndTime.formattedValue, "HH:MM:SS")
  3.  
  4. if (tStart ne 0 and tEnd ne 0) then
  5. //if start time is lower than end time.
  6. if (tStart < tEnd) then
  7. Concat(Abs(tStart - tEnd) / (60 * 1000), " minutes")
  8. //If start time is higher than end time
  9. else
  10. Concat(Abs(1440 - Abs(tStart - tEnd) / (60 * 1000)), " minutes")
  11. endif
  12. endif

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

Tech264
Registered: Apr 4 2008
Posts: 111
Thank you Radzmar that's exactly what I needed.

Thank you again.