1 Reply Latest reply on Jan 12, 2013 8:53 AM by gkaiseril

    Calculate Difference Between h:MM and h:MM

    SethDA1

      Hello All!  Relatively new to JavaScript (have some C+ and VBScript experience, but not much).  I have a timesheet form that I would like to calculate the difference between the start time (TimeIn1) and stop time (TimeOut2).  Also in this mix is a break/lunch time (Break1).  I would like the total hours to populate in a field that is determined by a value in a drop down box.  Not sure if I am being clear or not, but if I could at least get some help with the script for calculating time, I might be able to figure out the rest.  Any help would be awesome!

       

      Seth

        • 1. Re: Calculate Difference Between h:MM and h:MM
          gkaiseril MVP & Adobe Community Professional

          Have you searched for time sheets?

           

          There should be plenty of hits.

           

          Date and time fields' values are formatted character strings and not numbers. The fields also only contain the a valid string for the formatted type of time.

           

          Dates will only have a valid month within a year, dates will be only the valid dates for the month and year, time will only be between 12:00:00 am/12:00 and 11:59:59 pm/23:59:59. This also means that you can format a time field and set a value greater than 23:59:59, so the time format cannot be used to report elapsed time values equal to or more than 24 hours.

           

          You first need to convert the formatted string to a number before you can do any calculation.

           

          You use the utit.scand method to convert a date time string to the number of milliseconds since the Epoch date of Jan 1, 1970 12:00:00 am UTC. Date without time can be converted, time string need a date string associated with it for conversion.

          Once you get the milliseconds you can then do the necessary math as you want.

           

          It is also possible to use the RegExp to convert a time string only to a numeric value.

           

          I assume your time sheet is more than one row of data. Since you will be doing 2 conversions for each row with only the time value changing, you might want to use a function to convert the string values to numbers.

           

          I would link to the Acrobat JavaScript documents and the MDC JavaScript documents.

           

           

          // possible doucment level scripts
          function HHMM2Num(cFormat, cTime) {
          // convert time string to minutes
          // add default date string to time string
          var cDate = "1-Jan-1970 " + cTime;
          // convert to date time object
          var oDate = util.scand("dd-mmm-yyyy " + cFormat, cDate);
          // get time in milliseconds
          var nDate = oDate.getTime();
          // convert to minutes ( 1000 milliseconds in second, 60 seconds in minute)
          return Math.floor(nDate / (1000 * 60));
          } // end HHMM2Num

          function Num2hMM(nMin) {
          // convert minutes to HH:MM format
          // get the whole hours
          var nHrs = Math.floor(nMin / 60);
          // compute the remaining minutes
          var nMins = nMin % 60;
          // set field value string to formatted time string
          return util.printf("%,201.0f:%,202.0f", Math.floor(nMin / 60), nMin % 60);
          } // end Num2hMM
          // end document level scripts

           

           

          // custom calculation script for one row
          // get the values of the fields
          var cStart = this.getField("StartTime").value;
          var cEnd = this.getField("EndTime").value;
          // clear field as a default action
          event.value = "";
          // compute ony if we have values
          if(cStart != "" && cEnd != "") {
          // convert time strings to minutes using HHMM2Num function
          var nStart = HHMM2Num("h:MM tt", cStart);
          var nEnd = HHMM2Num("h:MM tt", cEnd);
          // compute the difference in milliseconds
          var nDiff = nEnd - nStart;
          // place any adjustment here
          // nDiff -= 30; // thirty minute lunch
          // format computed value
          event.value = Num2hMM(nDiff);
          } // end have values
          // end custom calculation script

           

          You will now need to compute the total of all rows.