jquery - 适用于假期和周末的 MVC 日期选择器

标签 jquery datepicker

日期选择器

<script type="text/javascript">
        var array = ["2015-11-25", "2015-11-27", "2015-11-29"]

        $(function () {
            $(".datepicker").datepicker({ beforeShowDay: $.datepicker.noWeekends, minDate: 0 })
            //$("#startDate").datepicker({ minDate: new Date(), beforeShowDay: $datepicker.noWeekends });
            $("#startDate").datepicker();
        });

        $('input').datepicker({
            beforeShowDay: function (date) {
                var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
                return [array.indexOf(string) == -1]
            }
        });

       $(function () { $('#startDate').datepicker({ beforeShowDay: $.datepicker.noWeekends }); });
    </script>

HTML

                <div class="form-group">
                    <div class="col-md-4 col-md-offset-4">
                        <label>Start Date</label>
                        @*<input type="date" class="form-control" id="startDate"/>*@
                        @Html.TextBox("datetime.now", String.Format("{0:d}", "Start date"), new { @class = "datepicker", type = "text" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-4 col-md-offset-4">
                        <br />
                        <label>End Date</label>
                        @*<input type="date" class="form-control" id="endDate" />*@
                        @Html.TextBox("01-01-2015", String.Format("{0:d}", "End date"), new { @class = "datepicker", type = "text" })
                    </div>
                </div>

尝试使用日期选择器来禁止选择周末和数组中的特定日期。我如何将两者结合起来?因为目前只有 noWeekends 有效。仅当我注释掉 noWeekends 部分时,才不允许选择数组中的日期。

最佳答案

var holidays_dates = ["11-25-2015", "11-27-2015", "11-29-2015","10-9-2015"];
 $(function() {
 $( "#startDate" ).datepicker({
    beforeShowDay: function(date){
            // First convert the date in to the mm-dd-yyyy format 
            // Take note that we will increment the month count by 1 
             var current_date = (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getFullYear() ;
 //alert(current_date)
             // We will now check if the date belongs to    holidays_dates array 
             if ($.inArray(current_date, holidays_dates) != -1 ) {
                 return [false];
             } 
             // In case the date is not present in holidays_dates array, we will now check if it is a weekend. 
             // We will use the noWeekends function
             var weekend_dates = $.datepicker.noWeekends(date);
             return weekend_dates; 
         }
         });
 });

关于jquery - 适用于假期和周末的 MVC 日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797022/

相关文章:

javascript - 如何根据选定的日期获取图表上的数据(使用日期选择器过滤数据图表JS)

jQuery 动画滚动 : Can I limit the function to specific targets?

javascript - 显示横幅加载程序,直到加载背景图像(使用 css)。使用 jquery 或 js

javascript - 使用 jQueryeach 循环遍历嵌套对象

javascript - 我的 css 类没有通过,使用 JQuery toggleClass

javascript - Keith-Wood 日期选择器 onclick(首次单击时不起作用)

java - 替代已弃用的 java date.sethours

javascript - Modernizr.Load 未捕获的类型错误

Javascript 检查所有 toggleclass

javascript - 日期范围选择器获取结束范围内的当前日期