6 Replies Latest reply on Dec 16, 2013 2:47 AM by MrFranciscoVaz

    time calculation

    MrFranciscoVaz

      hello,

      i have a form in which i need to calculate time:

       

      ex:

       

      01:30 + 01:30 + 01:30 = 04:30

       

      how can i do this ?

        • 1. Re: time calculation
          gkaiseril MVP & Adobe Community Professional

          Convert the time in hours and minutes to minutes, sum the minutes, and then convert back to hours and minutes with formatting as needed.

           

          Is this a string that needs to be parsed or are the values the values of fields?

           

           

          // times to add;
          var aTimes=  new Array("01:30", "01:30",  "01:30");
          // array for hours and minutes of a single time value;
          var aTime = new Array();
          // total elapsed time in minutes;
          var sum = 0;

          // process each element in the time array;
          for(i = 0;  i < aTimes.length; i++) {
          aTime = aTimes[i].split(":"); // split time string to hours and minutes;
          sum += (Number(aTime[0]) * 60) + Number(aTime[1]); // summ as minutes;
          } // next time value;
          console.println("total minutes: " + sum);
          console.println("Time : " + util.printf("%,0 0.0f", Math.floor(sum /60)) + ":" + util.printf("%,002.0f", (sum % 60)));

          • 2. Re: time calculation
            MrFranciscoVaz Level 1

            Hello GKaiseril,

            Thank you for your reply.

             

            The script calculates time between two or more fields ?

            Because i have two field to display time, and a thirth one for the total time.

            Of course that  times are different all the time.

            ex:

             

            - Time.1 + Time.2 = TotalTime

             

            - 1:55    +   1:35   = 3:30

             

            or,

             

            - 2:30 + 1:30 + 01:00 = 05:00

             

            The script calculate this? Because its not working, what i'm doing wrong?

             

             

            Thanks in advance...

            • 3. Re: time calculation
              gkaiseril MVP & Adobe Community Professional

              Clearly and fully state your problem. You asked for totaling a list of time strings.

               

              If you want to access fields, the you need to access the field object and get the field object's value.

               

              Under what conditions should the calculation be performed?

               

              Are you using LiveCycle for your forms?

               

              A working link to the form is very helpful.

              • 4. Re: time calculation
                MrFranciscoVaz Level 1

                Hello again GKaiseril,

                 

                Thanks again for your reply.

                 

                No i'm not using the LiveCycle, just the Acrobat 9 Pro "add or edit fields" menu.

                What I need is a script to calculate the total time form a row.

                Time.1 + Time.2 + Time.3 + Time.4 + Time.5 .... = TotalTime

                 

                Thanks,

                 

                calculate time.jpg

                • 5. Re: time calculation
                  gkaiseril MVP & Adobe Community Professional

                  The following code is reusible if the field names are changed, expanded or contracted for the field names and number of fields.

                   

                  // document level scripts;
                  function GetField(cName) {
                  // get field object with error reporting;
                  var oField = this.getField(cName);
                  if(oField == null) {
                    app.alert("Error accessing field " + cName, 0, 0);
                    }
                  return oField;
                  } // end GetField function;
                   
                  function Time2Num(cString) {
                  // convert time string in hh:MM format to minutes;
                  var nMins = 0;
                  if(cString.toString() != "") {
                    // split string into hours and minute strings;
                    var aString = cString.split(":");
                    // compute hours to minutes and add minutes;
                    nMins = (aString[0] * 60) + Number(aString[1]);
                  }
                  return nMins;
                  } // end Time2Num function;

                  function Num2Time(nMinutes) {
                  // format nMinutes to h:MM format;
                  var cTime = "";
                  if(nMinutes != 0) {
                    cTime = util.printf("%,101.0f", nMinutes /60) + ":" + util.printf("%,102.0f", nMinutes % 60);
                    }
                  return cTime;
                  } // end Num2Time function;
                  // end document level functions;

                  // custom calculation scirpt to total time fields;
                  // array of field names to process
                  var aFields = new Array("Text.1", "Text.2", "Text.3", "Text.4", "Text.5");
                  // some housekeeping to make sure the necessary functions exist;
                  if(typeof GetField != "function") app.alert("Missing GetField funciton.", 0, 0);
                  if(typeof Time2Num != "function") app.alert("Missing Time2Num function.", 0, 0);
                  var nSum = 0; // total of time values;
                  var oTime; // field object for field being processed;
                  // process each field in the array of field names;
                  for(i = 0; i < aFields.length; i++) {
                  oTime = GetField(aFields[i]);
                  if(oTime == null || oTime.valueAsStirng == "") break; // reject error or empty field;
                  if(oTime.vlaueAsString == "") break;
                  nSum += Number(Time2Num(oTime.valueAsString)); // add time to total;
                  } // next field in array;
                  event.value = "";
                  if(nSum != 0) {
                  event.value = nSum;
                  }
                  // end custom calculation script;

                  // custom format scrpt;
                  if(typeof Num2Time != "function") app.alert("Missing Num2Tme function.", 0, 0);
                  if(event.value != 0) {
                  event.value = Num2Time(event.value); // format event value;
                  }
                  // end custom format script;

                  • 6. Re: time calculation
                    MrFranciscoVaz Level 1

                    Hello GKaiseril,

                     

                    Thanks again.

                    I'm a little confused in the script.

                     

                    Can i just send to you the form via mail ?

                     

                    Thanks,