如何删除datepicker中的禁用日期计数

时间:2015-07-06 06:50:11

标签: javascript jquery datepicker

这是我的设计代码。我试图在这些天选择期间获得日期计数金额。通常我会正确得到日期计数。但是当我在禁用日期内选择日期范围时,我无法获得实际数量。请帮我。

var arrayable = ['2015/07/22', '2015/07/23'];

$('#txtDate1, #txtDate2').datepicker({
    minDate: 0,
    dateFormat: 'yy/mm/dd',
    beforeShowDay: function (date) {

    var datestring = jQuery.datepicker.formatDate('yy/mm/dd', date);
        if ($.inArray(datestring, arrayable) != -1 ) {
             return [false];
         } 
    var weekenddate = $.datepicker.noWeekends(date);
     return weekenddate; 
    }
});



    $('button').click(function() {
        var start   = $('#txtDate1').datepicker('getDate');
        var end = $('#txtDate2').datepicker('getDate');
        var days   = (end - start)/1000/60/60/24;
        alert(days);
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
From :<input type='text' id='txtDate1' /> <br/>
To :<input type='text' id='txtDate2' />
   <br><button>number of days</button>

2 个答案:

答案 0 :(得分:1)

最后我找到了答案。我希望有一天这会对某人有所帮助。我禁用了周末,特定日期来自阵列。之后我删除了所有选择的禁用日期计数和选择区域。看看这个jsfiddle。

var arrayable = ['2015-08-25','2015-08-26'];

function disableSpecificDates(date){
 var datestring = jQuery.datepicker.formatDate('yy-mm-dd', date);
    for(var i =0; i<arrayable.length;i++){
    	if($.inArray(datestring,arrayable)!=-1){
    	return[false];
    	}
    }
var weekend= $.datepicker.noWeekends(date);
    return weekend;
}

 $('#txtDate1, #txtDate2').datepicker({
    minDate :0,
    dateFormat: 'yy-mm-dd',
    beforeShowDay:  disableSpecificDates
 });

   $('button').click(function() {
        var start   = $('#txtDate1').datepicker('getDate');
        var end = $('#txtDate2').datepicker('getDate');
        var totalBusinessDays = 0;
               // normalize both start and end to beginning of the day
        start.setHours(0, 0, 0, 0);
        end.setHours(0, 0, 0, 0);
        var current = new Date(start);
        current.setDate(current.getDate() + 1);
        var day;
        while (current <= end) {
        day = current.getDay();
         var mystring = jQuery.datepicker.formatDate('yy-mm-dd', current);    
        if (day < 1 || day > 5 || $.inArray(mystring,arrayable)!=-1) {
       
            ++totalBusinessDays;
        }
	     current.setDate(current.getDate() + 1);
        }
 var days   = (((end - start)/1000/60/60/24)-totalBusinessDays);
        
       alert(days);
    });
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 <script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.22/jquery-ui.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

From :<input type='text' id='txtDate1' /> <br/>
To :<input type='text' id='txtDate2' />
   <br><button>number of days</button>


答案 1 :(得分:0)

没有内置函数可以告诉您日期选择器中是否禁用了日期。但是你可以添加这里定义的功能; http://bugs.jqueryui.com/ticket/7279#comment:5到jquery-ui.js。

仅在日期启用时,迭代日期并递增计数器。用法在这里定义; https://forum.jquery.com/topic/check-whether-a-ui-datepicker-date-is-disabled