7 Replies Latest reply on Oct 13, 2012 1:53 PM by gkaiseril

    Adding 2 Time formulas in Textboxes to get a total time

    deannehsts007

      I have been trying to add 2 time calculations together to get a total time rounded to the minute, but have been unsucessful.

       

      What I have right now in field "TimeIn1 and TimeOut1" is the calculation on click "$=Num2Time(Time())"

       

      I am trying

      to make it so that the person using the form can click in the Time In boxes and have the time automatically entered. Then I want to add 4 fields together

      to get a total time spent on a project.

       

      The 4 fields are;

      TimeIn1

      TimeOut1

      TimeIn2

      TimeOut2

       

      Total Time

       

      I need it to do the equasion like this;

      (Timein1+(TimeOut1-TimeIn2)+TimeOut2)/60=TotalTime

       

      Can anyone help me.Is the easiest way to run the formula as a formcalc or Is a Java Script better.

       

      Please help this newbie.

        • 1. Re: Adding 2 Time formulas in Textboxes to get a total time
          gkaiseril MVP & Adobe Community Professional

          The code you are using looks like FormCalc used in LiveCycle Designer and not JavaScript used in AcroForms used by Acrobat.

           

          Are you creating a form in LiveCycle Designer or Acorbat?

           

          Both programs are part of the Windows version of Acorbat Professional/Extended but the forms created by these programs are not compatilble at the design stage.

          • 2. Re: Adding 2 Time formulas in Textboxes to get a total time
            deannehsts007 Level 1

            Sorry, the form is being created in Livecycle designer.

            Thank you

            • 3. Re: Adding 2 Time formulas in Textboxes to get a total time
              gkaiseril MVP & Adobe Community Professional

              The 'Time2Num()' function converts a time string to the number of milliseconds from the Epoch date for LiveCycle Designer. There is an optional parameters for a format string and locale identifier string either of which may make the conversion more reliable as the function will know the format of the string. I have found using the 'formattedValue' property also helps ensure reliability.

               

              // get times in milliseconds
              var In1 = Time2Num(TimeIn1.formattedValue, "HH:MM")
              var Out1 = Time2Num(TimeOut1.formattedValue, "HH:MM")
              var In2 = Time2Num(TimeIn2.formattedValue, "HH:MM")
              var Out2 = Time2Num(TimeOut2.formattedValue, "HH:MM")
              // compute elapsed time in hours for the in and out times
              ((Out1 - In1) + (Out2 - In2)) / (1000 * 60 * 60)

              • 4. Re: Adding 2 Time formulas in Textboxes to get a total time
                deannehsts007 Level 1

                Thank you so much for your help!!

                • 5. Re: Adding 2 Time formulas in Textboxes to get a total time
                  jess.us

                  Hi GKaiseril,

                  I have been trying to get an answer to the above question, and your answer looks the closest I have gotten so far, however I cannot get the total to show anything ... If still around, is there any settings that obstruct any other number than 0 to show in the total?

                   

                  Pleeeeeease help!

                  • 6. Re: Adding 2 Time formulas in Textboxes to get a total time
                    gkaiseril MVP & Adobe Community Professional

                    And where do you want the time to show and in what format?

                     

                    The above code is for LvieCycle Designer. If you want to use it in Acrobat you will need to write a Time2Min function. and do some formatting.

                    • 7. Re: Adding 2 Time formulas in Textboxes to get a total time
                      gkaiseril MVP & Adobe Community Professional

                      Here is a script that will do the task in Acrobat, you will have to change the field names and formats to match your form:

                       

                      // start time
                      // Start time field name
                      var cStartName = "StartTime";
                      // Start time format:
                      var cStartFormat = "HH:MM";

                      // end time
                      // end time field name
                      var cEndName = "EndTime";
                      // End time format:
                      var cEndFormat = "HH:MM";

                      // some conversion functions
                      // function to convert time string to minutes
                      function Time2Num(cTime, cFormat) {
                      // convert time string to minutes
                      // convert time string to date object
                      var oDate = util.scand("mm/dd/yyyy " + cFormat, "01/01/1970 " + cTime);
                      // convert date object to number of milliseconds
                      var nTime = oDate.getTime();
                      // convert milliseconds to minutes and return value
                      return Math.round(nTime / (1000 * 60));
                      } // end Time2Num function

                      function Num2Time(nMinutes) {
                      // convert time string to Hours and Minute
                      // hours in nMinues
                      var nHrs = Math.floor(nMinutes / 60);
                      // remainder minutes from hours in nMinutes
                      var nMins = nMinutes % 60;
                      // return formatted time string
                      return util.printf("%,101.0f" + ":" + "%,102.0f", nHrs, nMins);
                      } // end Num2Time
                      // end conversion functions

                      // perform the calculation using the fields:
                      // get the values of the fields
                      var cStartTime = this.getField(cStartName).value;
                      var cEndTime = this.getField(cEndName).value;
                      // clear the value
                      event.value = "";

                      // test for not null time strings
                      if(cStartTime != "" && cEndTime != "") {
                      // compute the time difference in minutes
                      nDiff = Time2Num(cEndTime, cEndFormat) - Time2Num(cStartTime, cStartFormat);

                      event.value = Num2Time(nDiff);
                      } //end not null values