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

Sum of fields, in TIME FORMAT

eabikhzer
Registered: Jul 8 2008
Posts: 29

Hello,

I am a beginner and have a really quick question. I have searched for hours on forums and on the internet but can't seem to get the right script. I am using Acrobat Pro.

I have fields called: "TotalTime1", "TotalTime2", "TotalTime3", ... etc.. up until "TotalTime21". These fields are all in HH:MM format. I want to add these fields and have the result displayed in HH:MM format.

Please helpppppp

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
To do this you have to convert them all into some standard quantity type, like minutes. Then add all the minutes together and reconvert into hours and minutes.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

eabikhzer
Registered: Jul 8 2008
Posts: 29
Thanks for the answer..

I have very little knowledge of javascript and have not found any scripts in the forums and on the internet. Does anyone have a script that is already written?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Unless you are going to pay for custom work you'll have to learn a little JavaScript. Here's an example of what your script should look like, assuming this is on an AcroForm and not a LiveCycle form.

var cTime1 = this.getField("TotalTime1").value;
...
...
var aTime1 = cTime1.split(":");
var nMinutes1 = Number(aTime1[0])*60 + Number(aTime1[1]);
....
....
var nTotalMinutes = nMinutes1 + nMinutes2 + ... ;
var nHours = Math.floor(nTotalMinutes/60);
var nMinutes = nTotalMinutes%60;

That's it.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

eabikhzer
Registered: Jul 8 2008
Posts: 29
It just doesn't seem to work. I am willing to pay someone (Paypal) who will write me a working code to add up the field "TotalTime1" + "TotalTime2" ...until "TotalTime21" all in HH:MM format, and give me the result, in the same HH:MM format in a field called "SumofTimes". This shouldn't take someone who knows javascript more than 10 minutes,,,

eabikhzer [at] gieinc [dot] caThanks
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
A null entry is being taken to be zero which is Jan 1, 1900 in LiveCycle Designer. So, you hae to make sure there is data are not null or have data. There is the "HasValue()" function in FormCalc that will be true if a field has not had data entered and this combined with the "if" statement will restrict the calculation to be only performed when data has been entered.

if (HasValue(SourceDate.rawValue) then
Day2.rawValue =Num2Date(Date2Num(SourceDate.rawValue,"YYYY-MM-DD")
+ 1)
eles
null
endif

George Kaiser

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
One other bit, it seems like you are using a calculation script? if this is correct that do not use "Day2.rawValue" for the "Day2" field calculation script. Instead simply do the calculation. As George shows above with the "null" value. A field is automatically assigned the last value run in a Calculation Event.

And Please, post new questions to new threads. Hijacking someone else's thread isn't nice :(

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/]http://www.adobe.com/devnet/acrobat/[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I am sorry, there is an unbalanced ")".

The line:

if (HasValue(SourceDate.rawValue) then

Should be:

if (HasValue(SourceDate.rawValue) ) then

George Kaiser

eabikhzer
Registered: Jul 8 2008
Posts: 29
Can I have some hellp out of pitty since my thread was hijacked? :(
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Thom Parker provided the code for one field, you just need to repeat this for all fields.

// get the value of time fields 1 - 21
var cTime1 = this.getField("TotalTime1").value;
var cTime2 = this.getField("TotalTime2").value;
... // repeat for 3 - 20
var cTime21 = this.getField("TotalTime21").value;

// split the time fields value into an array of hours and minutes
// convert the hours to minutes and compute the total minutes as a variable
var aTime1 = cTime1.split(":");
var nMinutes1 = Number(aTime1[0])*60 + Number(aTime1[1]);
var aTime2 = cTime2.split(":");
var nMinutes2 = Number(aTime2[0])*60 + Number(aTime2[1]);
.... // repeat for 3 - 20
var aTime21 = cTime21.split(":");
var nMinutes21 = Number(aTime21[0])*60 + Number(aTime21[1]);

// add all the miutes variables
var nTotalMinutes = nMinutes1 + nMinutes2 + ... nMinutes21; // fillin the 3 - 20 times
// get the whole hours from the total of all minutes
var nHours = Math.floor(nTotalMinutes/60);
// get jus the minutes less than 1 hour (60 minutes) for the total of all minutes
var nMinutes = nTotalMinutes%60;
// make a string variable of the hours, ":", and minutes
var sTotalAllTime = nHours + ":" + nMinutes;

// display a literal and total time
console.show();
console.clear();
console.println("All time total: " + sTotalAllTime);
app.alert("All time total: " + sTotalAllTime, 0, 1);

event.value = sTotalAllTime; // fillin the field's value

George Kaiser

eabikhzer
Registered: Jul 8 2008
Posts: 29
Thank you so much gkaiseril ,

The script works perfectly, there is just one small thing...
The result has only one decimal after the ":". For example: 3:4 instead of 3:45...

Is there any way to fix this?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The creation of the time string needs to format the miutes to have a leading zero if the minute value is less than 10 minutes.

// build time strings or hours, ":", and minutes with leading zero
var sTotalAllTime = nHours + ":" + util.printf("%,102.0f", nMinutes);

George Kaiser

eabikhzer
Registered: Jul 8 2008
Posts: 29
Hello,

This Javascript works perfectly, there is just one minor detail.
It only works when the fields are completed, and those that are not completed are filled in as 0:00.
However, as soon as one or more fields are empty, the result doesn't show and I get "NaN:01" as a result for the total time. Is there a way to make it so that even if some fields are empty, it will ignore them and give me a sum of only the fields that are filled in?

Thank You!