Skip navigation
Currently Being Moderated

Annual Leave - Using JavaScript

Nov 9, 2010 3:40 AM

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?

 
Replies
  • Currently Being Moderated
    Nov 9, 2010 4:49 AM   in reply to rb0yd

    I have a sample form that calcs the diff between two dates. Please see the attached.

     

    Paul

    Attachments:
     
    |
    Mark as:
  • Currently Being Moderated
    Nov 9, 2010 5:48 AM   in reply to rb0yd

    There is a function in Javascript that will tell you what day if the week a certain date is ......so then you woudl have to calculate how many weekends and holidays there are in between those two dates and make the adjustment as neccessary. I do not know of any function for that but search the web there may already be a solution to this.

     

    Paul

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2012 4:03 AM   in reply to rb0yd

    Hello, I have selected this topic because I have a problem with dates.

    I have to calculate the difference in months between two dates, how can I do?

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points