2 Replies Latest reply on Aug 18, 2015 9:41 AM by taylorl42900175

    Time Sheet Calculations

    taylorl42900175

      I have a time sheet that I have been working on calculations for and I cannot seem to get any of the calculations to work. I am currently working on the form in adobe acrobat which requires me to use javascript which is way out of my element.

      Above is an example of the time sheet I am trying to get calculations for. The guys who fill it out need to be able to input the time as regular time and not in military time to make things easier for them. However, there are 3 time sections to fill out and based off that it should give a total number of hours worked but when entering times I receive negative numbers or it doesn't add properly like shown below.

      In the picture it shows that the total hours put in =.40 when it should really =12hrs.

      So far this is what I have for javascript but that is only for 1 in and out period (time section ex: morning in/out).

       

      function Time2Num(sTime, cFormat) {

      // convert a date time string to seconds since the epoch date

      var oTime = util.scand(cFormat, sTime); // date time object

      var fTime = oTime.valueOf(); // time string to milliseconds

      return Math.round(fTime / 1000); // return rounded seconds

      }


      function TimeDiff(sEnd, sStart) {

      // compute difference in time within same day as minutes

      var fEnd = 0; // default value if not data

      var fStart = 0; // default value if not data

      var cDateFormat = "m/d/yyyy "; // date format string

      var sNowDate = util.printd(cDateFormat, new Date()); // get today's date as "m/d/yyyy'

      var cDateTimeFormat = cDateFormat + "h:MM tt";


      // compute difference if we have data

      if(sEnd != "" & sStart != "") {

      // get end time in minutes

      var cDateTimeValue = sNowDate + sEnd; // start date time string

      var fEnd = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec

      // get start time in minutes

      var cDateTimeValue = sNowDate + sStart; // start date time string

      var fStart = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec

      } // end if we have value

      return fEnd - fStart; // compute and return the difference in minutes

      } // end function


      function Mins2HrsMins(fMinutes) {

      // convert seconds into formatted string of hours:minutes

      var cDisplayFormat = "%01.0f" + ":" + "%02.0f"; // format for returned value

      var fHrs = Math.floor(fMinutes / 60); // compute whole hours from the difference

      var fMins = fMinutes % 60; // get minutes less than 1 hour form the difference

      return util.printf(cDisplayFormat, fHrs, fMins); // format and return computed value

      } // end function


      var fDiff = TimeDiff(this.getField("Time Out 1").value, this.getField("Time In 1").value)

      event.value = Mins2HrsMins(fDiff); // format computed value

       

      Could someone please help me out and write a code for me that will make the hours calculate correctly? I am willing to try anything because at this point I cannot figure out the calculations to save my life! Thanks Much!

        • 1. Re: Time Sheet Calculations
          gkaiseril MVP & Adobe Community Professional

          Do you understand how to code custom JavaScript ?

           

          Do you understand what a function is and how it works.

           

          Do your field names by row match the field names used in the functions?

           

          Where have you placed the various pieces of code?

           

          It looks like you have 3 functions. One converts a time string to the number of seconds since the Epoch date. The next uses the first function to convert the start time and end time into minutes from the Epoch date and compute the difference in minutes .The 3rd function reformats the minutes into hours and minutes for display.

           

          The last bit of code computes the formatted value for the time worked for 1 interval.

           

          You have 3 intervals for one day so you need to compute the time worked for each interval and sum those values to get the total time for the day.

           

          For your example using the functions:

           

           


          InMorning= "7:30 am";
          OutMorning = " 12:00 am";
          InAfternoon = "12:30 am";
          OutAfternoon = "8:00 pm"
          var nMorningWorked = TimeDiff(OutMorning, InMorning);
          console.println("Morning minutes worked: " + nMorningWorked);
          var nAfternoonWorked = TimeDiff(OutAfternoon, InAfternoon);
          console.println("Afternoon minutes worked: " + nAfternoonWorked);
          var nWorked = TimeDiff(OutMorning, InMorning) + TimeDiff(OutAfternoon, InAfternoon);
          console.println("Total minutes Worked: " + nWorked);
          console.println("Time worked: " + Mins2HrsMins(nWorked));

           

          Produces the following results:


          Morning minutes worked: 270
          Afternoon minutes worked: 450
          Total minutes Worked: 720
          Time worked: 12:00

          true

          • 2. Re: Time Sheet Calculations
            taylorl42900175 Level 1

            Thank you so much! I think I kind of understand your example but I am not sure.

             

            To answer your questions I do not know how to write javascript or understand it at all... the only reason why I had some form of code is because I took it from another form that only had time in and out and changed the heading names to match mine. I figured I might be able to use the code that had already been made and add the 2 other sections to it but that did not work.