Search code examples
jquerydatepickerjquery-multidatespicker

multiDatesPicker - Only allow one day every 2 weeks from Current Day


I have the mulitdatespicker, and I need to disable ALL dates except those that are every 2 weeks from the "defaultDate".

So if the current day is set to thursday 6th May, I need to disable all days before it, then all the days up to Thurs 20th May (2 weeks from the 6th). Then disable 2 weeks again up to 3rd June... (2 weeks from the 20th) etc.

So the only dates selectable are every 2 weeks from the defaultDate.

jQuery('.multidatepicker').multiDatesPicker({
    dateFormat: "d M yy", 
    defaultDate: '6 May 2021',
    maxDate: 72,
    minDate: new Date(),
    addDisabledDates: ??? //Do I add something here?
}); 


Solution

  • you could do that: push disabled dates in an array

    var defaultDate = new Date("6 May 2021");
    var datedeb = new Date(defaultDate);
    var maxDate = 72;
    
    var arDate=[];
    var enable = false;
    
    for(var i = 1; i <maxDate; i++)
    {
        enable = i % 14 == 0;
        var newdate = new Date(datedeb.setDate(datedeb.getDate() + 1));
        if(!enable)arDate.push(newdate);  
    
    }
    
    jQuery('.multidatepicker').multiDatesPicker({
        dateFormat: "d M yy", 
        defaultDate: defaultDate,
        maxDate: maxDate,
        minDate: defaultDate,
     addDisabledDates: arDate
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <link href="https://code.jquery.com/ui/1.12.1/themes/pepper-grinder/jquery-ui.css" rel="stylesheet"/>
    
    <script src="https://cdn.rawgit.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.js"></script>
    <link href="https://cdn.rawgit.com/dubrox/Multiple-Dates-Picker-for-jQuery-UI/master/jquery-ui.multidatespicker.css" rel="stylesheet"/>
    <div class ="multidatepicker" id="mdp-demo"></div>