Bootstrap - Datepicker /禁用数组中的特定日期

时间:2017-01-24 14:09:48

标签: javascript jquery arrays twitter-bootstrap datepicker

我有一个包含日期的数组。我想循环此数组并禁用datepicker控件中的可选日期。 我需要在beforeShowDay参数中执行此操作!

这是我的代码:

var datesForDisable = ["25-01-2017", "26-01-2017", "27-01-2017"]

$("#holidayDateFrom").datepicker({
            format: 'dd-mm-yyyy',
            autoclose: true,
            weekStart: 1,
            calendarWeeks: true,
            todayHighlight: true,
            //datesDisabled: datesForDisable,
            //daysOfWeekDisabled: [0, 6],

            beforeShowDay: function (currentDate) {
                var dayNr = currentDate.getDay();

                if (dayNr == 6) {//This works
                    return false;
                }

                if (dayNr == 0) {//This works
                    return false;
                }



                     // This dosnt works..
                        var dateNr = moment(currentDate.getDate()).format("DD-MM-YYYY");
                        if (datesForDisable.length > 0) {
                            for (var i = 0; i < datesForDisable.length; i++) {
                                var date = new Date(datesForDisable[i]);
                                if (date == dateNr) {
                                    return false;
                               }
                            }
                        }
                        return true;
                    }
                })

怎么做?谢谢你以前!

2 个答案:

答案 0 :(得分:1)

datesDisabled: datesForDisable

我不确定你为什么评论这个选项。根据您的问题,此选项提供您所寻找的相同内容。看看:

http://jsfiddle.net/CxNNh/4056/

它对我来说非常适合

var datesForDisable = ["03-04-2019", "03-09-2019", "03-15-2019"];

   $(document).ready(function () {
        $('.datepicker').datepicker({

            multidate: true,
            format: 'mm-dd-yyyy',
            todayHighlight: false,
            datesDisabled: datesForDisable,
            multidateSeparator: ',  ',
            templates : {
                leftArrow: '<i class="fi-arrow-left"></i>',
                rightArrow: '<i class="fi-arrow-right"></i>'
            }
        });
    });

答案 1 :(得分:0)

您可以将日期作为时间戳(unix时间)进行比较

var datesForDisable = ["25.01.2017", "26.01.2017", "27.01.2017"]

$("#datepicker").datepicker({
            format: 'dd/mm/yyyy',
            autoclose: true,
            weekStart: 1,
            calendarWeeks: true,
            todayHighlight: true,
            //datesDisabled: datesForDisable,
            //daysOfWeekDisabled: [0, 6],

            beforeShowDay: function (currentDate) {
                var dayNr = currentDate.getDay();
                var dateNr = moment(currentDate.getDate()).format("DD-MM-YYYY");

                if (dayNr == 6) {//This works
                    return false;
                }

                if (dayNr == 0) {//This works
                    return false;
                }
                    if (datesForDisable.length > 0) {
                        for (var i = 0; i < datesForDisable.length; i++) {                        
                            if (moment(currentDate).unix()==moment(datesForDisable[i],'DD.MM.YYYY').unix()){
                                return false;
                           }
                        }
                    }
                    return true;
                }
            })