Search code examples
jquerydatepickerjquery-ui-datepicker

Highlight, Disable specific days in jQuery UI datepicker


Here, i want to achieve following

  1. Highlight all holidays in date-picker
  2. Disable specific days in date-picker
  3. Disable specific weekends in date-picker

I have snippets of each separately, but i want all together

Fiddle:Working Demo

Highlight holidays

var holydays = ['10/17/2013', '10/18/2013', '11/2/2013'];

function highlightDays(date) {
    for (var i = 0; i < holydays.length; i++) {
        if (new Date(holydays[i]).toString() == date.toString()) {
            return [true, 'highlight'];
        }
    }
    return [true, ''];

}

$(function () {
    $("#dp").datepicker({
        minDate: 0,
        dateFormat: 'mm/dd/yy',
        inline: true,
        numberOfMonths: [1, 2],
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        beforeShowDay: highlightDays,
    });
});

Disable specific days

var disabledDays = ["10-20-2013", "10-21-2013", "11-15-2013", "11-17-2013"];
    function disableAllTheseDays(date) {
        var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
        for (i = 0; i < disabledDays.length; i++) {
            if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
                return [false];
            }
        }
        return [true];
    }

Disable Weekends

$(function() {
        $( "#availability" ).datepicker({
            minDate: 0,
            dateFormat: 'mm/dd/yy',
            inline: true,
            numberOfMonths: [1, 2],
            dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            beforeShowDay: $.datepicker.noWeekends
        });
    });

Could anyone help me..

Thanks in advance.


Solution

  • change :

    $(function () {
        $("#dp").datepicker({
            minDate: 0,
            dateFormat: 'mm/dd/yy',
            inline: true,
            numberOfMonths: [1, 2],
            dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
            beforeShowDay: setCustomDate // <-----change function
        });
    });
    

    add function :

    function setCustomDate(date) {
        var clazz = "";
        var arr1 = highlightDays(date);
        if (arr1[1] != "") clazz = arr1[1];
    
        var arr2 = disableAllTheseDays(date);
        var arr3 = $.datepicker.noWeekends(date);
    
        return [(!arr2[0] || !arr3[0]) ? false : true, clazz];
    }