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

前端 未结 11 674
無奈伤痛
無奈伤痛 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:48

    The datepicker has this functionality built in!

    $( "#datepicker" ).datepicker({
      beforeShowDay: $.datepicker.noWeekends
    });
    

    http://api.jqueryui.com/datepicker/#utility-noWeekends

    0 讨论(0)
  • 2020-11-22 04:50

    If you don't want the weekends to appear at all, simply:

    CSS

    th.ui-datepicker-week-end,
    td.ui-datepicker-week-end {
        display: none;
    }
    
    0 讨论(0)
  • 2020-11-22 04:53

    These answers were very helpful. Thank you.

    My contribution below adds an array where multiple days can return false (we're closed every Tuesday, Wednesday and Thursday). And I bundled the specific dates plus years and the no-weekends functions.

    If you want weekends off, add [Saturday], [Sunday] to the closedDays array.

    $(document).ready(function(){
    
        $("#datepicker").datepicker({
            beforeShowDay: nonWorkingDates,
            numberOfMonths: 1,
            minDate: '05/01/09',
            maxDate: '+2M',
            firstDay: 1
        });
    
        function nonWorkingDates(date){
            var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
            var closedDates = [[7, 29, 2009], [8, 25, 2010]];
            var closedDays = [[Monday], [Tuesday]];
            for (var i = 0; i < closedDays.length; i++) {
                if (day == closedDays[i][0]) {
                    return [false];
                }
    
            }
    
            for (i = 0; i < closedDates.length; i++) {
                if (date.getMonth() == closedDates[i][0] - 1 &&
                date.getDate() == closedDates[i][1] &&
                date.getFullYear() == closedDates[i][2]) {
                    return [false];
                }
            }
    
            return [true];
        }
    
    
    
    
    });
    
    0 讨论(0)
  • 2020-11-22 04:53

    The solution here that everyone likes seems to very intense... personally I think it's much easier to do something like this:

           var holidays = ["12/24/2012", "12/25/2012", "1/1/2013", 
                "5/27/2013", "7/4/2013", "9/2/2013", "11/28/2013", 
                "11/29/2013", "12/24/2013", "12/25/2013"];
    
           $( "#requestShipDate" ).datepicker({
                beforeShowDay: function(date){
                    show = true;
                    if(date.getDay() == 0 || date.getDay() == 6){show = false;}//No Weekends
                    for (var i = 0; i < holidays.length; i++) {
                        if (new Date(holidays[i]).toString() == date.toString()) {show = false;}//No Holidays
                    }
                    var display = [show,'',(show)?'':'No Weekends or Holidays'];//With Fancy hover tooltip!
                    return display;
                }
            });
    

    This way your dates are human readable. It's not really that different it just makes more sense to me this way.

    0 讨论(0)
  • 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 });
    
    0 讨论(0)
提交回复
热议问题