在jquery中的日期选择器上启用一周的前一个日期

时间:2018-03-16 07:28:34

标签: javascript jquery datepicker calendar

我正在尝试禁用datepicker上的所有日期,除了当前日期之前7天的日期,但它会禁用所有日期。当我取消注释注释的代码行并注释else部分"返回true"线。 但它也会禁用周末。那么如何在不使用noweekends(currentdate)

的情况下实现它
$(document).ready(function () {
        $(function () {
            {
                var currentDate = new Date();
                $("#StartDate").datepicker({ beforeShowDay: DisableSpecificDates });
                $("#StartDate").datepicker({ dateFormat: 'yy-mm-dd' }).val();
                $("#StartDate").datepicker("setDate", currentDate);

            }
        });

        $(function () {
            {

                var currentDate = new Date();
                $("#StartDte").datepicker({ beforeShowDay:DisableSpecificDates });
                $("#StartDte").datepicker("setDate", currentDate);
            }
        });
        function DisableSpecificDates(currentdate) {

            //var weekenddate = $.datepicker.noWeekends(currentdate);
            var difference;
            var minDate = new Date();
            var todaydate = new Date();
            var d = todaydate.getDate();
            var prevdays = 0;
            difference = d - 6;
            if (d <= prevdays && d > 0) {
                minDate.setDate(difference);
            }
            else {
                minDate.setDate(difference);
            }
            var datemin =new Date (minDate);
            var day = todaydate.getDay();
            switch (day)
            {
                case 0:
                case 1:

                    prevdays = 4;
                    break;

                case 2:
                case 3:
                case 4:
                case 5:

                    prevdays = 2;
                    break;

                case 6:
                    prevdays = 3;
                    break;
            }
            datemin.setDate(datemin.getDate()+1);
            var dateOfMin = (minDate.getDate()) - prevdays - 1;
            minDate.setDate(dateOfMin);
            if (currentdate > todaydate || currentdate < minDate) 
            {
                return false;
            }
            return true;
            //return weekenddate;    
        }

1 个答案:

答案 0 :(得分:0)

好像你想设置日期范围,如下图所示,所以下面将修复日期范围并禁用周末

$( "#datepicker" ).
   datepicker({ minDate: -20, maxDate: "+1M +10D", beforeShowDay: $.datepicker.noWeekends });

check datepicker : Date Range

或尝试这样

$('#date-time-picker').datepicker({
    format: 'YYYY-MM-DD',
    useCurrent: false,
    showClose: true,
    minDate: '2018-03-09',
    maxDate: '2018-03-15',
    beforeShowDay: $.datepicker.noWeekends 
})

但是在这里你需要从脚本中提供思想和最大日期,为了简单起见我硬编码日期