How to disable a specific date range in bootstrap daterangepicker?

I want to disable a specific date range in bootstrap daterangepicker.

I need to use bootstrap daterangepicker, which gives me the ability to limit a specific date range.

+4
source share
2 answers

To disable dates, use datesDisabledmethod to provide an array.

Some dates are disabled in this CodePen .

$("#picker").datepicker({
    datesDisabled:["11/24/2016","11/28/2016","12/02/2016","12/23/2016"]
});



EDIT

The previous answer was for Bootstap DatePicker ...
Sorry for the wrong reading, my bad.

Bootstrap 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();

CodePen.



EDIT ;)

, .
( ), , .

, :

$("#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!");
    }
});

CodePen .

+3

Louys Patrice Bessette - .

, 1 , , .

startDate<disabledArr[i] && endDate>disabledArr[i]

Date ,

startDate = new Date(picker.startDate.format('MM/DD/YYYY'))
endDate = new Date(picker.endDate.format('MM/DD/YYYY'))

disableArr

+3

Source: https://habr.com/ru/post/1661141/


All Articles