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

# Annual Leave - Using JavaScript

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?

• ###### 1. Re: Annual Leave - Using JavaScript

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

Paul

• ###### 2. Re: Annual Leave - Using JavaScript

Thanks Paul.  This is a useful starting point.

The matter is complicated though by the insistence of the form owner that the form is able to automatically adjust the calculated result to ignore weekends and specified public holidays.

Is there a way to find the number of working/business days?

• ###### 3. Re: Annual Leave - Using JavaScript

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

• ###### 4. Re: Annual Leave - Using JavaScript

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?