【问题标题】:How to check whether a date is disabled or not in Bootstrap Datepicker?如何在 Bootstrap Datepicker 中检查日期是否被禁用?
【发布时间】:2018-04-15 17:39:54
【问题描述】:

我的表单中有三个 <input> 元素。

<input id="from-date" type="text" class="form-control" placeholder="From">
<input id="to-date" type="text" class="form-control" placeholder="To">
<input id="total" class="form-control" placeholder="Total no. of days">

其中 first 和 second 接受从 Bootstrap Datepicker 中选择的日期,最后一个显示总天数。

计算的总天数不包括周末(周六和周日)。我现在想实现一个功能,当我使用datesDisabled 选项禁用一组日期时,这些禁用的日期不应计入总数。天。如何检查 Bootstrap Datepicker 中的日期是否被禁用?

这是我的代码的快速JS Fiddle

下面是我的 JS。

$(function() {
            var date = new Date();
            var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
            var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());

            // create the from date
            $('#from-date').datepicker({
                autoclose: true,
                format: 'dd-mm-yyyy',
                startDate: today,
                daysOfWeekDisabled: [0,6],
                datesDisabled:["12-04-2018","17-04-2018","19-04-2018"],
            }).on('changeDate', function(ev) {
                ConfigureToDate();
            });


            $('#to-date').datepicker({
                autoclose: true,
                format: 'dd-mm-yyyy',
                daysOfWeekDisabled: [0,6],
                datesDisabled:["12-04-2018","17-04-2018","19-04-2018"],
                startDate: $('#from-date').val()
            }).on('changeDate', function(ev) {
                var fromDate = $('#from-date').data('datepicker').dates[0];
                var get_no_of_days = getWorkingDatesCount(fromDate, ev.date);
                var final_count = parseInt(get_no_of_days) + 1;//adding +1 to the total number of days to count the present date as well.
                $('#total').val(final_count);
            });

            // Set the min date on page load
            ConfigureToDate();

            // Resets the min date of the return date
            function ConfigureToDate() {
                $('#to-date').val("").datepicker("update");
                $('#to-date').datepicker('setStartDate', $('#from-date').val());
            }
        });

        function getWorkingDatesCount(startDate, endDate) {
            var count = 0;
            var curDate = new Date(startDate);
            while (curDate <= endDate) {
                var dayOfWeek = curDate.getDay();
                if ( !((dayOfWeek == 6) || (dayOfWeek == 0)) )
                count++;
                curDate.setDate(curDate.getDate() + 1);
            }
            return count;
        }

如果有人能帮我解决这个问题,那将是非常有帮助的。

【问题讨论】:

    标签: javascript jquery html bootstrap-datepicker


    【解决方案1】:

    工作示例:https://jsfiddle.net/cCrul/qLt6k0yv/

    我刚刚将datesDisables 声明为变量:

    var datesDisabled = ["12-04-2018", "17-04-2018", "19-04-2018"];
    

    在执行count++之前,我用它来检查curDate是否在那个数组中:

    if (
        !((dayOfWeek == 6) || (dayOfWeek == 0)) &&
        (datesDisabled.indexOf(formatDate(curDate)) == -1)
    ) {
        count++;
    }
    

    在jsfiddle代码中定义的formatDate()函数。

    $(function() {
      var date = new Date();
      var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
      var end = new Date(date.getFullYear(), date.getMonth(), date.getDate());
    
      var datesDisabled = ["12-04-2018", "17-04-2018", "19-04-2018"];
      // create the from date
      $('#from-date').datepicker({
        autoclose: true,
        format: 'dd-mm-yyyy',
        startDate: today,
        daysOfWeekDisabled: [0, 6],
        datesDisabled: datesDisabled,
      }).on('changeDate', function(ev) {
        ConfigureToDate();
      });
    
    
      $('#to-date').datepicker({
        autoclose: true,
        format: 'dd-mm-yyyy',
        daysOfWeekDisabled: [0, 6],
        datesDisabled: datesDisabled,
        startDate: $('#from-date').val()
      }).on('changeDate', function(ev) {
        var fromDate = $('#from-date').data('datepicker').dates[0];
        var get_no_of_days = getWorkingDatesCount(fromDate, ev.date);
        var final_count = parseInt(get_no_of_days) + 1; //adding +1 to the total number of days to count the present date as well.
        $('#total').val(final_count);
      });
    
      // Set the min date on page load
      ConfigureToDate();
    
      // Resets the min date of the return date
      function ConfigureToDate() {
        $('#to-date').val("").datepicker("update");
        $('#to-date').datepicker('setStartDate', $('#from-date').val());
      }
    
      function getWorkingDatesCount(startDate, endDate) {
        var count = 0;
        var curDate = new Date(startDate);
        while (curDate <= endDate) {
          var dayOfWeek = curDate.getDay();
          if (!((dayOfWeek == 6) || (dayOfWeek == 0)) && (datesDisabled.indexOf(formatDate(curDate)) == -1)) {
            console.log(formatDate(curDate));
            count++;
          }
          curDate.setDate(curDate.getDate() + 1);
        }
        return count;
      }
    
      function formatDate(date) {
        var d = new Date(date),
          month = '' + (d.getMonth() + 1),
          day = '' + d.getDate(),
          year = d.getFullYear();
    
        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;
    
        return [day, month, year].join('-');
      }
    });
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker.standalone.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.min.js"></script>
    
    
    <input id="from-date" type="text" class="form-control" placeholder="From">
    <input id="to-date" type="text" class="form-control" placeholder="To">
    <input id="total" class="form-control" placeholder="Total no. of days">

    【讨论】:

    • 完美!感谢您的解决方案@raul.vila
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多