Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?

前端 未结 11 798
無奈伤痛
無奈伤痛 2020-11-22 04:02

I use a datepicker for choosing an appointment day. I already set the date range to be only for the next month. That works fine. I want to exclude Saturdays and Sundays f

11条回答
  •  滥情空心
    2020-11-22 04:54

    In the latest Bootstrap 3 version (bootstrap-datepicker.js) beforeShowDay expects a result in this format:

    { enabled: false, classes: "class-name", tooltip: "Holiday!" }
    

    Alternatively, if you don't care about the CSS and tooltip then simply return a boolean false to make the date unselectable.

    Also, there is no $.datepicker.noWeekends, so you need to do something along the lines of this:

    var HOLIDAYS = {  // Ontario, Canada holidays
        2017: {
            1: { 1: "New Year's Day"},
            2: { 20: "Family Day" },
            4: { 17: "Easter Monday" },
            5: { 22: "Victoria Day" },
            7: { 1: "Canada Day" },
            8: { 7: "Civic Holiday" },
            9: { 4: "Labour Day" },
            10: { 9: "Thanksgiving" },
            12: { 25: "Christmas", 26: "Boxing Day"}
        }
    };
    
    function filterNonWorkingDays(date) {
        // Is it a weekend?
        if ([ 0, 6 ].indexOf(date.getDay()) >= 0)
            return { enabled: false, classes: "weekend" };
        // Is it a holiday?
        var h = HOLIDAYS;
        $.each(
            [ date.getYear() + 1900, date.getMonth() + 1, date.getDate() ], 
            function (i, x) {
                h = h[x];
                if (typeof h === "undefined")
                    return false;
            }
        );
        if (h)
            return { enabled: false, classes: "holiday", tooltip: h };
        // It's a regular work day.
        return { enabled: true };
    }
    
    $("#datePicker").datepicker({ beforeShowDay: filterNonWorkingDays });
    

提交回复
热议问题