How to determine number Saturdays and Sundays comes between two dates in java script

后端 未结 5 548
余生分开走
余生分开走 2020-12-06 13:32

I have requirement as follows I have two dates i need to find how may saturdays and sundays will come in between
Date1: 02/06/2011
Date2: 02/07/2011
10 days are

5条回答
  •  长情又很酷
    2020-12-06 14:37

    Edited to count number of weekend days instead of number of weekends. http://jsfiddle.net/bRgUq/3/

    function CalculateWeekendDays(fromDate, toDate){
        var weekendDayCount = 0;
    
        while(fromDate < toDate){
            fromDate.setDate(fromDate.getDate() + 1);
            if(fromDate.getDay() === 0 || fromDate.getDay() == 6){
                ++weekendDayCount ;
            }
        }
    
        return weekendDayCount ;
    }
    
    console.log(CalculateWeekendDays(new Date(2011, 6, 2), new Date(2011, 7, 2)));
    

提交回复
热议问题