10 Replies Latest reply on Nov 27, 2017 4:49 PM by gkaiseril

    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;