Highlight, Disable specific days in jQuery UI datepicker

倾然丶 夕夏残阳落幕 提交于 2019-11-27 06:25:19

问题


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.


回答1:


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];
}



回答2:


We are now in 2014, and a minimal code is required. Here is my way to disable days.

my variable 'specific' , 0 <= specific <= 7

in my demo :

1 = sunday 2 = saturday 4 = Monday to friday

if specific == 5 that mean 4 + 1 so i allow Monday till friday + sunday.

7 means all days allowed to pick. notice that I only allow to pick starting now to Month + 1

$( "#datepicker" ).datepicker({
    minDate:0,
    maxDate: '+1M-1D',
    dateFormat: "yy-mm-dd",
    beforeShowDay: function(date) {
        var day = date.getDay();
        if(day == 6)
            return [(specific & 2) > 0, ''];
        else if(day == 0)
            return [(specific & 1) > 0, ''];
        else
            return [(specific & 4) > 0, ''];
    }
});



回答3:


Just for future references. Since I was looking for the same thing and found these same function to disable days in a lot of different places:

http://www.spiceforms.com/blog/how-to-disable-dates-in-jquery-datepicker-a-short-guide/ https://davidwalsh.name/jquery-datepicker-disable-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];
}

But since you're using inArray, the for is not needed, resulting in this simpler and more efficient function:

var disabledDays = ["4-2-2016", "4-4-2016"];

function disableAllTheseDays(date) {
     var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
     if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) return [false];
     return [true];
}



回答4:


This one works for me. Placed before closing body tag.

function DisableMonday(date) {
      var day = date.getDay();
     if (day == 1) {
     return [false,"","Unavailable"] ; 
     } else { 
     return [true] ;
     } 
    }
    jQuery(function($){
        $('input[name="datum"]').datepicker('option', 'beforeShowDay', DisableMonday).datepicker('refresh');
    });


来源:https://stackoverflow.com/questions/19395558/highlight-disable-specific-days-in-jquery-ui-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!