3 Replies Latest reply on Apr 25, 2011 10:52 AM by LgilesXT1

    Newbie to scripting - calculate time difference - HELP

    Chris448

      Hi, I've 3 fields (Start, End, Total). Start & End are time fields. I need Total to display the minutes of (End - Start). I thought I could enter the script below as the Validation for End so that once that field was tabbed out of it would run & display the Total minutes but nothing happens. Please, please help & THANKS!!!

       

      {
      var t2 = (document.getElementById('End').value), t1 = (document.getElementById('Start').value);
      var m = (t2.substring(0,t2.indexOf(':'))-0) * 60 +
              (t2.substring(t2.indexOf(':')+1,t2.length)-0) -
              (t1.substring(0,t1.indexOf(':'))-0) * 60 +
              (t1.substring(t1.indexOf(':')+1,t1.length)-0);
      document.Total.value = m/60;
      }

        • 1. Re: Newbie to scripting - calculate time difference - HELP
          gkaiseril MVP & Adobe Community Professional

          Have you looked at the Acrobat JavaScript API Reference?  JavaScript for Acrobat

           

          Do not confuse this scripting language with Java or web JavaScript, they are not the same.

           

          One way to do the computation using the RegExp to convert the time strings in hours and minutes with a ':' separator, covers both civilian and 24 hour time formats:

           

          function Time2Min(cTime){
          var min = 0
          cTime = cTime.toLowerCase();
          // civilian time
          if (/(\d{0,2})[:](\d{2})[ ]?([ap])/.test(cTime)) {
          if (RegExp.$3 == "p") {
          RegExp.$1 = Number(RegExp.$1) + 12;
          }
          if(RegExp.$1 == 12 & RegExp.$3 == "a") {
          min = 0;
          } else {
          min = 60 * Number(RegExp.$1);
          }
          min += Number(RegExp.$2);
          } else {
          // military time
          if (/(\d{0,2})[:](\d{2})/.test(cTime)) {
          min = 60 * Number(RegExp.$1);
          min += Number(RegExp.$2);
          }
          }
          return min;
          } // end function


          // variable for the elapsed time
          var elapsed = 0;
          // enter the name for the start and end fields
          var cStart = "TimeIn_1";
          var cEnd = "TimeOut_1";
          // get the values for the start and end fields
          var sStart = this.getField(cStart).value;
          var sEnd = this.getField(cEnd).value;
          if (sEnd != "" & sStart !="") {
          elapsed += (Time2Min(sEnd)  - Time2Min(sStart)) ;
          }


          event.value = elapsed;

          • 2. Re: Newbie to scripting - calculate time difference - HELP
            Chris448 Level 1

            Nope, but I will now. HUGE thanks!!

            • 3. Re: Newbie to scripting - calculate time difference - HELP
              LgilesXT1

              GKaiseril,

               

              You seem to be very well versed in this topic.

               

              I used the scripting below in my companies Timesheet PDF and found it to be succesfull.

              I am now trying to achieve some things using this script. I will gladly pay you for

              your time in assisting me in accomplishing some of the added features.Pay/Pal

               

              Here is what I am trying to achieve.

               

              1. I would like it to calculate in hours:Minutes instead of just Minutes.

               

              2. I would like for it to be able to recongnize AM/PM time frames

              currently it goes into a negative calculation if it goes from AM to PM.

               

              Thank you very much for your time.

               

              You can reach me at lorenzogiles@gmail.com

               

              Lorenzo