2017-02-17 93 views
0

我正在使用dangrossman daterangepicker。我找到了一種方法來禁用在選擇器這樣的週六和週日:在沒有周末的情況下在daterangepicker中計算天

isInvalidDate: function(date){ 
return (date.day() == 0 || date.day() == 6);} 

然後我計算天數是這樣的:

function(start, end, label) 
    { 
     console.log(label); 
     var hd = end.diff(start, 'days'); 
     $('#totalDays').val(hd); 
    }); 

但它仍包括週六和週日在計算。有沒有辦法排除它?謝謝!

+0

可能ü可以在這裏找到你的解決方案:http://stackoverflow.com/a/3464346/2651863 –

+0

看起來像daterangepicker使用了moment.js。你可以適應這一點:http://stackoverflow.com/questions/20788411/how-to-exclude-weekends-between-two-dates-using-moment-js –

回答

0

對於蠻力的方法,你可以數着日子到達結束一天,直到和排除週末:

function(start, end, label) 
{ 
    // I don't know if this check is needed, but just to be sure 
    if(start.isAfter(end)){ 
     var swap = start.clone(); 
     start = end; 
     end = swap; 
    } 
    var counter = start.clone(); 
    var days = 1; 
    while(!counter.isSame(end)){ 
     if(counter.day() != 0 && counter.day() != 6) 
     { 
      days++; 
     } 
     counter.add(1,"days"); 
    } 
    $('#totalDays').val(days); 
});