7 Replies Latest reply on Jul 26, 2015 6:24 PM by KrisHunt

    Calculating elapsed hours between two time fields

    KrisHunt Level 4

      I have a form with fields TimeStart and TimeEnd. Both are formatted as Time fields of the format h:MM tt. I also have a field called Hours with the format of Number. Below is the Javascript code I have in the Custom Calculation Script section of the Hours field. Could anyone tell me why it's not working? This script started out as a (working) date calculation script, and I modified it for calculating hours instead of days.

       

      var strStart = this.getField("TimeStart").value;

      var strEnd = this.getField("TimeEnd").value;

      if (strStart.length && strEnd.length)

      {

        var timeStart = util.scand("h:MM tt",strStart);

        var timeEnd   = util.scand("h:MM tt",strEnd);

        var diff = timeEnd.getTime() - timeStart.getTime();

        var oneHour = 60 * 60 * 1000;

        var hours = Math.floor(diff/oneHour);

        event.value = hours;

      }

      else event.value = 0;

        • 1. Re: Calculating elapsed hours between two time fields
          try67 MVP & Adobe Community Professional

          An hour is not enough information to create a Date object. It must also contain an actual date.

          • 2. Re: Calculating elapsed hours between two time fields
            KrisHunt Level 4

            So you're saying you can't use the util.scand function with any of the time formats Acrobat provides?

            • 3. Re: Calculating elapsed hours between two time fields
              try67 MVP & Adobe Community Professional

              Not unless you specify an arbitrary date, no. You did that before, though, why did you take it away?

              • 4. Re: Calculating elapsed hours between two time fields
                gkaiseril MVP & Adobe Community Professional

                It is possible to split a time string without a date but if the user at a locality that uses DST or Summer Time and the time period covered includes a period including the time of the change, the calculation will not be correct. Using the date and time string will properly include the affect of the local time change. Without a date string there is no way to know if the calculation needs to be adjusted for the DST or Summer Time adjustment.

                 

                The date object has a value of the number of milliseconds since Midnight January 1, 1970 at the Royal Observatory at Greenwich, England. This value is not adjusted for time zones nor local time adjustment for DST or Summer Time. The location was chosen since the Royal Observatory is the location for coordinating time world wide using the UTC format, formerly GMT. This is similar to how you computer keeps time. You computer uses a network time server to synchronize to the UTC value and then applies the time zone offset as set on your computer and adjust for DST or Summer Time also as set by you system along with some OS code.

                • 5. Re: Calculating elapsed hours between two time fields
                  KrisHunt Level 4

                  Okay, I have a totally different script now that works well to calculate the number of elapsed hours between two times. I have a new problem, though. I have a test form with three sets of times, and three Subtotal fields that contain the elapsed number of hours for each set. I want to add up the total number of hours in a Grand Total field at the bottom. Each Subtotal calculates correctly, but the Grand Total field does not update until you enter some data in some other new field. If all the fields are already full, then it won't update at all. Any idea what's wrong? Here's a link to the test PDF:

                   

                  parseTime

                   

                  And here's the code in the PDF:

                  /* Document Level Function */

                   

                  function parseTime(cTime)

                  {

                    if (cTime == '') return null;

                    var d = new Date();

                    var time = cTime.match(/(\d+)(:(\d\d))?\s*(p?)/);

                    d.setHours( parseInt(time[1]) + ( ( parseInt(time[1]) < 12 && time[4] ) ? 12 : 0) );

                    d.setMinutes( parseInt(time[3]) || 0 );

                    d.setSeconds(0, 0);

                    return d;

                  }

                   

                  /* Code for each Subtotal */

                   

                  var cStart = this.getField("Start 1").value;

                  var cStop = this.getField("Stop 1").value;

                   

                   

                  if (cStart != "" && cStop != "") {

                      var tStart = parseTime(cStart);

                      var tStop = parseTime(cStop);

                    var total = (tStop - tStart)/(1000*60*60);

                   

                      event.value = (total < 0 ? total += 24 : total);

                  }

                  else {

                      event.value = "";

                  }

                   

                  The Grand Total field is just a simple sum of the three Subtotals.

                  • 6. Re: Calculating elapsed hours between two time fields
                    gkaiseril MVP & Adobe Community Professional

                    The order for the calculation of fields is wrong, you need to update it.

                    • 7. Re: Calculating elapsed hours between two time fields
                      KrisHunt Level 4

                      Got it. I wasn't aware there was a field calcualtion order option until now.