javascript 具有自定义设置的两个 jquery datepicker 字段之间的天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13261933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number days between two jquery datepicker fields with customized settings
提问by John C. Derrick
My jquery datepicker fields set a min and max date for the start and end fields, and that seems to throw off my attempts to write a script to calculate the number of days between the selected dates. I know others have asked this question, but every fix I've tried here on stackoverflow has failed to work, I suspect because of the customizations I have in my code. Can anyone please help me figure out how to take the code below and make it provide me a value in an input field called #totaldays? Thanks
我的 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)
});
Thanks!
谢谢!
回答by adeneo
I've created this function that I usually run in the onSelect
function of the datepickers to calculate the days between them :
我创建了这个函数,我通常在onSelect
日期选择器的函数中运行它来计算它们之间的天数:
function days() {
var a = $("#datepicker_start").datepicker('getDate').getTime(),
b = $("#datepicker_end").datepicker('getDate').getTime(),
c = 24*60*60*1000,
diffDays = Math.round(Math.abs((a - b)/(c)));
console.log(diffDays); //show difference
}