我有2个输入,其中选择了日期#startdate和#enddate。我正在使用datepicker,目前已经禁用了周末和假期,这非常有效!
然后我有一个#days输入来计算两个日期之间的差异,这个日期差异然后有一个周末计数。
我想创建一个假期计数,所以我也可以从天数的不同中减去这个。
所以我最终会以
结束$('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1);
我当前的代码如下(这已经从其他stackoverflow问题中获得并且效果很好:)向下是var holidaycount,这正是我所挣扎的。任何帮助将不胜感激。
//holidays
var natDays = [
[1, 1, 'uk'],
[1, 2, 'uk'],
[1, 3, 'uk'],
[1, 4, 'uk'],
[12, 24, 'uk'],
[12, 25, 'uk'],
[12, 26, 'uk'],
[12, 27, 'uk'],
[12, 28, 'uk'],
[12, 29, 'uk'],
[12, 30, 'uk'],
[12, 31, 'uk']
];
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
// do initialization here
$("#startdate").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
firstDay: 1,
yearRange: '0:+100',
beforeShowDay: noWeekendsOrHolidays,
onSelect: function( selectedDate ) {
$("#enddate").datepicker("option","minDate",selectedDate );
$("#enddate2").datepicker("option","minDate",selectedDate );
},
minDate: '+1d',
maxDate: '+' + DAY_DIFFERENCE + 'd'
});
// do initialization here
$("#enddate").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
firstDay: 1,
yearRange: '0:+100',
beforeShowDay: noWeekendsOrHolidays,
maxDate: '+' + DAY_DIFFERENCE + 'd'
});
// do initialization here
$("#enddate2").datepicker({
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true,
firstDay: 1,
yearRange: '0:+100',
beforeShowDay: noWeekendsOrHolidays,
onSelect: function( selectedDate ) {
$d1 = $('#startdate').val();
$d2 = $('#enddate2').val();
$myDateParts1 = $d1.split("-");
$myDateParts2 = $d2.split("-");
$d1flip = new Date($myDateParts1[2], ($myDateParts1[1]-1), $myDateParts1[0]);
$d2flip = new Date($myDateParts2[2], ($myDateParts2[1]-1), $myDateParts2[0]);
$newdate1 = $d1flip.format("ddd mmm dd hh:MM:ss yyyy");
$newdate2 = $d2flip.format("ddd mmm dd hh:MM:ss yyyy");
// For Opera and older winXP IE n such
$d1new = Date.parse($newdate1);
$d2new = Date.parse($newdate2);
var weekend_count = 0;
for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
var temp = new Date(i);
if (temp.getDay() == 0 || temp.getDay() == 6) {
weekend_count++;
}
}
var holidaycount = 0;
for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
var temp = new Date(i);
if (**date in var natDays & is in selected difference range**) {
holidaycount++;
}
}
console.log(weekend_count);
console.log(holidaycount);
$('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1);
}
});
}, 'html');
return false;
});
}
修改
从答案中,我已经插入了这个功能并尝试了这个.... 的console.log(holidaycount);返回0,但选择的日期我选择它应该是10
var holidaycount = 0;
for (i = $d1new.valueOf(); i <= $d2new.valueOf(); i+= 86400000){
var temp = new Date(i);
if (isHoliday(temp)) {
holidaycount++;
}
}
答案 0 :(得分:1)
更新:根据评论修改getMonth()
至getMonth() + 1
。
**date in var natDays & is in selected difference range**
日期在选定的差异范围内始终为真,因为new Date(i)
在所述范围内(就像在weekend_count
的循环中一样:你没有费心去检查是否你在这个范围内。)
现在,检查temp
内的日期是否是假日,你可能想要使用一个函数(尽管你仍然可以在代码中直接执行):
/* dateObject is a JS Date object (e.g. dateObject = new Date();) */
/* country is the country we test holidays for */
function isHoliday(dateObject, country) {
/* let's assume natDays is global, otherwise pass it as a third argument to this function */
for(var i = 0; i < natDays.length; i++) {
/* natDays[i][0] is a day, natDays[i][1] is a month, natDays[i][2] is a country indicator */
if(parseInt(dateObject.getDate()) == parseInt(natDays[i][0]) && parseInt(dateObject.getMonth()) + 1 == parseInt(natDays[i][1]) && country.toLowerCase() == natDays[i][2].toLowerCase()) {
/* found a day and a month matching our current Date's day and month: interrupt and tell caller dateObject is a holiday */
return true;
}
}
/* end of loop, we parsed all possible holidays without finding any match for our Date: tell caller the given Date is not a holiday */
return false;
}
现在把它放在你的代码中:
if (isHoliday(temp, 'uk')) {
holidaycount++;
}
这应该可以解决这个问题,虽然它可能需要一些重构(没有测试这个代码),并且可能有更优雅的方法(比如修改Date对象的原型以使用此函数作为对象的方法) )。