4 Replies Latest reply: Apr 2, 2012 4:03 AM by noos76 RSS

    Annual Leave - Using JavaScript

    rb0yd Community Member

      I've had a look at this forum for a solution but have not found a satisfactory way to calculate the number of business days between two dates. I need to do this for an annual leave form.

       

      Searching online has uncovered a number of possible scripts that could help but they are primarily written in the context of an HTML page. A search in the help pages and Scripting Reference section in LCD seems to indicate that a key element of the script (getDay)will not work as it is not supported in LCD.

       

      function calcBusinessDays(dDate1, dDate2) {         // input given as Date objects
       
        var iWeeks, iDateDiff, iAdjust = 0;
       
        if (dDate2 < dDate1) return -1;                 // error code if dates transposed
       
        var iWeekday1 = dDate1.getDay();                // day of week
        var iWeekday2 = dDate2.getDay();
       
        iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1;   // change Sunday from 0 to 7
        iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
       
        if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;  // adjustment if both days on weekend
       
        iOriginalWeekday1 = iWeekday1;    // only count weekdays
        iOriginalWeekday2 = iWeekday2
       
        // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
        iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
       
        if (iOriginalWeekday1 <= iOriginalWeekday2 {
          iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
        } else {
          iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
        }
       
        iDateDiff -= iAdjust                            // take into account both days on weekend
       
        return (iDateDiff + 1);                         // add 1 because dates are inclusive
       
      }

       

      Is there an alternative scripting solution I can use to get around this?  Is there a LCD compatible JS version of the Excel NETWORKDAYS function?