【问题标题】:Calendar disable dates from next month日历禁用下个月的日期
【发布时间】:2021-04-03 12:25:14
【问题描述】:

我正在使用 JQuery 来阻止某些日期,但下个月可用。 用户可以点击今天 + 5 并且所有其他应该被禁用。 我做错了什么?

  <div id='datepicker' onchange="test(this)">
  </div>
        
    $('#datepicker').datepicker(
        {
            numberOfMonths: 2,
            beforeShowDay: function (date) {

              
                var hilight = [true, 'isActive'];
                var today = new Date();
                var blockdays = new Date();
               // var startdayofmonth = new Date(today.getFullYear(), today.getMonth(), 1);
                today.setDate(today.getDate() + 5);
                blockdays.setDate(blockdays.getDate() + 12);
                blockdays = moment(blockdays.toLocaleDateString(), 'MM.DD.YYYY').format('YYYY-DD-MM');
                var blockendofmonth = new Date(today.getFullYear(), today.getMonth() + 1, 1);;
                blockendofmonth = moment(blockendofmonth.toLocaleDateString(), 'MM.DD.YYYY').format('YYYY-DD-MM');
                today = moment(today.toLocaleDateString(), 'MM.DD.YYYY').format('YYYY-DD-MM');
                date = moment(date.toLocaleDateString(), 'MM.DD.YYYY').format('YYYY-DD-MM');
               
              
             
                if (date < today) {
                    hilight = [false, ''];
                }
                else if (date >= blockdays) {
                    hilight = [false, ''];
                }

                return hilight;
            }
        }

    );

【问题讨论】:

    标签: javascript jquery date calendar


    【解决方案1】:

    比较YYYY-DD-MM 格式的日期字符串会得到不正确的结果。 '2021-10-04' &lt; '2021-20-03' 这被评估为 true' 这实际上是不正确的。比较日期字符串时,最好遵循YYYY-MM-DD:HH:MM:SS 格式以获得更好的结果。在 sn-p 下方结帐。

    $(document).ready(function () {
        var allowDays = getAllowDays();
        $('#datepicker').datepicker({
            numberOfMonths: 2,
            beforeShowDay: function (date) {
                var hilight = [true, 'isActive'];
                var start = allowDays[0];
                var end = allowDays[1];
                var currentDate = +moment(date).format('YYYYMMDD');
                if (currentDate < start) {
                    hilight = [false, ''];
                } else if (currentDate >= end) {
                    hilight = [false, ''];
                }
                return hilight;
            }
        });
    });
    function getAllowDays() {
        var today = new Date();
        var blockdays = new Date();
        today.setDate(today.getDate() + 5);
        blockdays.setDate(blockdays.getDate() + 12);
        return [+moment(today).format('YYYYMMDD'), +moment(blockdays).format('YYYYMMDD')];
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.9.2/jquery.ui.datepicker.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css"/>
    <div id='datepicker'></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多