具有自定义设置的两个jquery datepicker字段之间的天数

时间:2012-11-07 01:17:46

标签: javascript jquery datepicker

我的jquery datepicker字段设置了开始和结束字段的最小和最大日期,这似乎摒弃了我编写脚本以计算所选日期之间的天数的尝试。我知道其他人已经问过这个问题了,但我在stackoverflow上尝试的每个修复都无法正常工作,我怀疑是因为我的代码中有自定义。谁能帮助我弄清楚如何获取下面的代码并让它在一个名为#totaldays的输入字段中为我提供一个值?感谢

// Calendar Dates
/* create an array of days which need to be disabled */
var disabledDays = ["11-13-2012","11-14-2012","11-15-2012","11-29-2012","11-30-2012"];

/* utility functions */
function nationalDays(date) {
  var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
  //console.log('Checking (raw): ' + m + '-' + d + '-' + y);
  for (i = 0; i < disabledDays.length; i++) {
    if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
      return [false];
    }
  }
  return [true];
}


//Block the Weekends
function noWeekendsOrHolidays(date) {
   var noWeekend = $.datepicker.noWeekends(date);
    if (noWeekend[0]) {
        return nationalDays(date);
    } else {
        return noWeekend;
    }
} 

$(document).ready(function () {
$.datepicker.setDefaults({dateFormat: 'mm/dd/yy',minDate: +1,changeMonth: true,changeYear: true,numberOfMonths: 2,constrainInput:true,beforeShowDay:nationalDays,});
        var selector = function (dateStr) {
            var d1 = $('#datepicker_start').datepicker('getDate');
            var d2 = $('#datepicker_end').datepicker('getDate');
            var diff = 0;
            if (d1 && d2) {
                diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
            }
            $('#totaldays').val(diff);
        }
$('#datepicker_start').datepicker({onSelect: function(selectedDate) {
    var minDate = $(this).datepicker('getDate');
    if (minDate) {minDate.setDate(minDate.getDate() + 3);}//min days requires
    $('#datepicker_end').datepicker('option', 'minDate', minDate || 1); // Date + 1 or tomorrow by default
}});
$('#datepicker_end').datepicker({minDate: 1, onSelect: function(selectedDate) {
    var maxDate = $(this).datepicker('getDate');    
    if (maxDate) {maxDate.setDate(maxDate.getDate() - 1);}
    $('#datepicker_start').datepicker('option', 'maxDate', maxDate); // Date - 1    
}});
    $('#datepicker_start,#datepicker_end').change(selector)
});

谢谢!

0 个答案:

没有答案