回答
要禁用日期,請使用datesDisabled
method來提供數組。
有些日期在this CodePen被禁用。
$("#picker").datepicker({
datesDisabled:["11/24/2016","11/28/2016","12/02/2016","12/23/2016"]
});
編輯
以前的答案是爲Bootstap的DatePicker ...
很抱歉的誤讀,是我不好。
以下是我已經找到了引導DateRangePicker:
// Define the disabled date array
var disabledArr = ["11/24/2016","11/28/2016","12/02/2016","12/23/2016"];
$("#picker").daterangepicker({
isInvalidDate: function(arg){
console.log(arg);
// Prepare the date comparision
var thisMonth = arg._d.getMonth()+1; // Months are 0 based
if (thisMonth<10){
thisMonth = "0"+thisMonth; // Leading 0
}
var thisDate = arg._d.getDate();
if (thisDate<10){
thisDate = "0"+thisDate; // Leading 0
}
var thisYear = arg._d.getYear()+1900; // Years are 1900 based
var thisCompare = thisMonth +"/"+ thisDate +"/"+ thisYear;
console.log(thisCompare);
if($.inArray(thisCompare,disabledArr)!=-1){
console.log(" ^--------- DATE FOUND HERE");
return true;
}
}
}).focus();
這在this CodePen工作。
編輯在評論獎金問題;)
以上,是畫與禁用日期的日曆。
現在,您需要的是再次比較所選範圍,對應用(當用戶選擇一個範圍時),禁止包含某些禁用日期的範圍。
因此,這裏是一個附加功能:
$("#picker").on("apply.daterangepicker",function(e,picker){
// Get the selected bound dates.
var startDate = picker.startDate.format('MM/DD/YYYY')
var endDate = picker.endDate.format('MM/DD/YYYY')
console.log(startDate+" to "+endDate);
// Compare the dates again.
var clearInput = false;
for(i=0;i<disabledArr.length;i++){
if(startDate<disabledArr[i] && endDate>disabledArr[i]){
console.log("Found a disabled Date in selection!");
clearInput = true;
}
}
// If a disabled date is in between the bounds, clear the range.
if(clearInput){
// To clear selected range (on the calendar).
var today = new Date();
$(this).data('daterangepicker').setStartDate(today);
$(this).data('daterangepicker').setEndDate(today);
// To clear input field and keep calendar opened.
$(this).val("").focus();
console.log("Cleared the input field...");
// Alert user!
alert("Your range selection includes disabled dates!");
}
});
它不能與daterangepicker一起使用。 –
哈...對不起。但你有沒有試過[isInvalidDate](http://www.daterangepicker.com/#options)?它看起來是同樣的選擇。 –
非常感謝。最後它的工作。 –
Louys Patrice Bessette的回答很棒。
我只是想讓它更加準確,因爲我發現了1個錯誤,當您在不同的月份中選擇開始日期和結束日期以及它們之間的禁用日期時,它不會提醒您。原因是這裏
startDate<disabledArr[i] && endDate>disabledArr[i]
你需要在比較之前解析到日期對象,也將是錯誤的這樣
startDate = new Date(picker.startDate.format('MM/DD/YYYY'))
endDate = new Date(picker.endDate.format('MM/DD/YYYY'))
類似於disableArr禁用的日期
謝謝
我剛剛看到你的帖子......感謝我的回答。 ;)+1 –
您是否厭倦了文檔中的'minDate'和'maxDate'? –
其實我希望它的橫幅購買,所以,它來與任何範圍。 –
任何範圍意味着什麼?你的意思是動態範圍? –