Check if weekend exist in date range using javascript

前端 未结 7 1819
我在风中等你
我在风中等你 2021-01-12 21:27

Wondering if anyone has a solution for checking if a weekend exist between two dates and its range.

var date1 = \'Apr 10, 2014\';
var date2 = \'Apr 14, 2014\         


        
7条回答
  •  再見小時候
    2021-01-12 21:40

    You are only checking if the first or second date is a weekend day.

    Loop from the first to the second date, returning true only if one of the days in between falls on a weekend-day:

    function isWeekend(date1,date2){
        var date1 = new Date(date1), date2 = new Date(date2);
    
        //Your second code snippet implies that you are passing date objects 
        //to the function, which differs from the first. If it's the second, 
        //just miss out creating new date objects.
    
        while(date1 < date2){
            var dayNo = date1.getDay();
            date1.setDate(date1.getDate()+1)
            if(!dayNo || dayNo == 6){
                return true;
            }
        }
    }
    

    JSFiddle

提交回复
热议问题