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

Subtracting Hours and Minutes

adobeone2010
Registered: Nov 23 2010
Posts: 27
Answered

I've had to deal with an earlier issue concerning time which was resolved. This time I wanted to do the same except for using the formcalc since everything I've read claims to be easy to use. So either I missed something or it's not as easy as it seems. Here is the problem:
 
I have two field boxes already containing times in HH:MM format. All I want to do is subtract one field box from the other. Simple arithmatic except that I'm dealing with times. I've already done the java route with the help of others here. So how do I use the formcalc?
 
I use the formcalc and all I get as an answer is "0"
 
field one = 10:30
field two = 8:30
field three should be 1:30
 
field one - field 2
 
all I get is "0" where it should be 1:30
 
So what I'm I doing wrong? with formcalc in livecycle adobe pro 8

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Are you sure about your computation result?

If you are getting zero then either the calculation order of the fields is wrong or you are not converting the time correctly to a number.

What are the names of the fields?

What is the "Data format" for each field?

Have you read the LiveCycle Scripting reference about date and time calculations?

George Kaiser

adobeone2010
Registered: Nov 23 2010
Posts: 27
That's my point, I've read how the time needs to be converted and have the Javascript for it. I'm specifically talking about the formcalc. I've read that it's an easy form to use for those who don't understand java (like me) Without going into Javascript. How would I use formcalc to achieve my required results? Keep in mind, that field box 1 and 2 already have their time in HH:MM format. All I need is to subtract one time field from the other to get the answer I'm seeking. See the above example.

Thanks
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi adobeone2010,

I believe in Formcalc that you still have to convert your time values into date and time objects in order for the calculations to work out. But don't take my word for it- Look it up in the official documentation as gkaiseril suggested.

Dimitri
WindJack Solutions
Online Acrobat JavaScript Training & Resource Library
WindJack Solutions
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Accepted Answer
I believe the difference in hours and minutes between 10:30 and 8:30 is 2 hours.

To access a field in any scripting language, one needs the field names. For FormCalc, field names can not contain spaces.

If one has the following fields:

DateTimeField1 - Data Format: Time, and a format of "HH:MM"
DateTimeField2 - Data Format: Time, and a format of "HH:MM"
TextField1 - calculate read only

The following "Calculation" in FormCalc can be used to compute the difference in hours and minutes between "DateTimeField1" and "DateTimeField2":

// get end time in milliseconds from the Epoch date
var EndTime = Time2Num(DateTimeField1.formattedValue, "HH:MM")
// get start time in milliseconds from Epoch date
var StartTime = Time2Num(DateTimeField2.formattedValue, "HH:MM")
// compute difference in milliseconds
var Diff = EndTime - StartTime
// difference in seconds
Diff = Diff / 1000
// difference in minutes
var Minutes = Diff / 60
// difference in hours
var Hours = Minutes / 60
// Get whole hours only
Hours = Floor(Hours)
// Get minutes only (modulo 60 of minutes)
Minutes = Mod(Minutes, 60)
// format the result
Concat(Hours, ":", Format("99", Minutes))

Comments begin with "//".

The milliseconds will be the milliseconds from the Epoch date and time adjusted for your computer's time zone offset.

You will have to change the field names and formats for your form.

The above script computes a difference of 2:00 for your example. If your end time is 10:00 and the start time is 9:00 the difference computed is 1:30.

George Kaiser

degracia1
Registered: May 9 2011
Posts: 6
good day
I need to find the difference in minutes between 2 datetime fields that I have on a single form. I need to store the result in a field called mindiff.

startDateAndTime : the user enters the value in this field in the following format dd/mm/yyyy hh:mm
endDateAndTime : the user enters the value in this field in the following format dd/mm/yyyy hh:mm

How can I do this in Livecycle , either using formcalc or javascript ?

at the end i need to convert the date and the time into minutes and subtract date from time. is that possible?

i have all these fields in 1 tables in a subform and i'm using FormCalc.
thanks for your assistance.