Calculating number of Days for Leave Management in JS

Hi, i have just started learning & understanding JavaScript.
I am just trying to do a small project for my Brother regarding Leave Management, for which its required to Select the From Date & To Date, wherein the Total no. of days should be calculated, excluding the weekends.

I have just worked around to calculate the number of days but i am not sure how to exclude the Weekends. & what if the the Year is a Leap year - So how will i include the Extra days of Feb month. I am using RAD date-pickers to get the Value & split it.

But being a newbie to the Coding world i m really N’joyin the JavaScript…

Just need help regarding this coz i m totally helpless…

I am taking the 2 Dates from 2 Rad Datepickers & diplaying the Number of Days in a Text Box.

Here is My Coding :-


 <script type="text/javascript">  

function CalcLeaveDays()
       {
            var FromDate=document.getElementById("ctl00_ContentPlaceHolder1_ApplyLeaveNew_control_rdpStartdate");
            var ToDate=document.getElementById("ctl00_ContentPlaceHolder1_ApplyLeaveNew_control_rdpEnddate");
            document.getElementById("ctl00_ContentPlaceHolder1_ApplyLeaveNew_control_txtnoofdays");
                    
            var weekday=new Array(7);
            weekday[0]="Sunday";
            weekday[1]="Monday";
            weekday[2]="Tuesday";
            weekday[3]="Wednesday";
            weekday[4]="Thursday";
            weekday[5]="Friday";
            weekday[6]="Saturday";
         
            var FrmDate=FromDate.value.substring(8,10);
            var FrmMonth=FromDate.value.substring(5,7);
            var FrmYear=FromDate.value.substring(0,4);
            var TDate=ToDate.value.substring(8,10);
            var TMonth=ToDate.value.substring(5,7);
            var TYear=ToDate.value.substring(0,4);
            var sysDate=new Date();
            var sysDay=sysDate.getDate();
            var sysMonth=(sysDate.getMonth())+1;           
            var sysYear=sysDate.getFullYear();
                 

            var LeaveDays;
                
            if(FrmYear>=sysYear)
            {
                if((FrmMonth>=sysMonth)&(TMonth>=sysMonth))
                {
                     if(FrmDate>(sysDay+3))
                     {
                        if(TDate>=FrmDate)
                        {
                            if(FrmDate==TDate)
                            {
                                alert ('1')
                                LeaveDays = 1;
                            }
                            else if(TDate>FrmDate)
                            {
                                alert(TDate-FrmDate+1);
                                LeaveDays = TDate-FrmDate+1;                                
                                                          
                            }
                            else
                            {
                                alert('Check');
                                return false;
                            }
                        }
                        else
                        {
                            alert('Kindly Select the End Date Appropriately ');
                            return false;
                            
                        }
                    }
                }
            }
    


	document.getElementById("ctl00_ContentPlaceHolder1_ApplyLeaveNew_control_txtnoofdays").value =LeaveDays;
             
       }

  </script>


Perhaps a couple of existing resources that do the job would help, so that you can gain inspiration from them.

http://refactormycode.com/codes/1156-business-days


Date.prototype.addBizDays = function(n){
    var day = this.getDay();
    var d =  new Date();
    var wkn = (n < 0 ? -2 : 2);
    d.setDate(this.getDate()+n+(day === 6 ? 2 : +!day) + (Math.floor((Math.abs(n)/5))*wkn));
    return d;
}

You would use it as follows:


var toDate = fromDate.addBizDays(numberOfDays);

An alternative business days function is also http://snipplr.com/view/4086/calculate-business-days-between-two-dates/

Thanks a Lot…

This has been of Great Help to me…!!!