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

Calculate Difference (minus) for Form Fields

knowgood
Registered: Aug 20 2008
Posts: 13
Answered

Hello,

I need to set up calculations between form fields, about which I know the very basics and that's why I'm surprised that what I need is not a default. Why is "Sum" a default choice but not "Difference"?

I want to tell Acrobat
Field_3 is calculated by subtracting Field_2 from Field_1

Apparently, I need a Javascript. Anyone know what this would look like or where I can go to find out? Thanks very much!

Alex Adams

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Have you read the answer to [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=15754]Calculate Difference (Minus) in Form Field[/url]?

George Kaiser

knowgood
Registered: Aug 20 2008
Posts: 13
Yes, I read that reply posting. In truth I found it unelucidating, although I definitely appreciate your time and attention. How do i write a custom script? How do i use simplified field notation? How do i know if my field names comply with simple name format? Lastly, what is eSeminar? Thank you!

Alex Adams
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The eSeminar on demand is about Acrobat forms. Then there is the How To JavaScript Corner article about Acrobat Froms Custom calculation scripts.

So for the field for the difference:

Simplified field notation:
FieldAName - FieldBName
Custom calculation script:
event.value = this.getField("FieldAName ").value - thie.getField("FieldBName").value;

George Kaiser

knowgood
Registered: Aug 20 2008
Posts: 13
Thank you SO MUCH! You are a real life saver. I have been pulling out my hair about this for days.

Cheers,
Alex Adams
RMatthews
Registered: Dec 30 2009
Posts: 3
I need to calculate the total time spent on site at a customers locations
I have a field "Start Time" and a field "Stop Time"
I would like to have the field "Total Time" calculated in Total Hours
I can not figure out the the correct Code

is this on the right track:
var c = this.getField("Stop Time").value;
var d = this.getField("Start Time").value;
event.value = c - d;

I have the Time Fields set to HH:MM (24 Hour Clock)
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Date and Time fields have a formatted value, you need to convert these formatted strings to JavaScript date time object, then extract the number of days from the epoch date, and then do the math.

Have you read Thom Parker's JavaScript tutorials about working with date and time in JavaScript?

George Kaiser

RMatthews
Registered: Dec 30 2009
Posts: 3
Yes I have but I am still a little confused
But I understand I need to Convert
"Start Time" field to Decimal
"Stop Time" field to Decimal
Then Calculate the Difference, Convert the "Difference" back to Hours and Minutes.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
A quick search turns up [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=22148]Calculating difference between date and time[/url].

The 'util.scand()' method converts the date time string to the date object.

The '.getTime()' method gets the number of milliseconds from the epoch date, or you use the 'valueOf()' method will also get the number of milliseconds from the epoch date.

You can then either convert the milliseconds minutes and calculate the difference or use milliseconds for the difference computation and then convert the difference to minutes.

You then need to use the 'round()' method of the 'Math' object to obtain the value of the minutes from the difference. You can then use the modulus, '%', operator to get the minutes minutes remainder from dividing by 60 minutes to the hour. Then use the 'Math.floor()' method to get the hours and days from the difference in minutes. You can then use these values to create a formatted string.

George Kaiser