计算日期之间的特定工作日并排除 Javascript 中的假期
Calculate specific weekdays between dates and exclude holidays in Javascript
从 this question 开始,它使用以下方法计算给定日期范围内特定工作日的数量:
function countCertainDays( days, d0, d1 ) {
var ndays = 1 + Math.round((d1-d0)/(24*3600*1000));
var sum = function(a,b) {
return a + Math.floor( ( ndays + (d0.getDay()+6-b) % 7 ) / 7 );
};
return days.reduce(sum,0);
}
我还想知道如果假期落在范围内的工作日,如何排除假期,假设我有一系列假期发生在 d0
和 d1
之间。
感谢帮助!
前段时间我写了类似的脚本也许对你有帮助:
var today = new Date();
var end = new Date(2016, 6, 1);
var allDays = Math.floor((end.getTime() - today.getTime())/ 86400000);
var holidays = [
[15, 8],[1, 10],[17, 10],[29,9],[30,9],[23,11],[24,11],[25,11],[26,11],[27,11],[28,11],[29,11],[30,11],[31,11],[1,0],[2,0], [3,0],[4,0],[5,0],[6,0],[7,0],[1,1],[31,0],[22,1],[23,1],[24,1],[25,1],[26,1],[24,2],[25,2],[26,2],[27,2],[28,2],[29,2],[1,4],[8,4],
];
var days = 0;
for(var i = 0; i < allDays; i++){
var tmpDate = new Date();
tmpDate.setTime(today.getTime() + i * 24*60*60*1000);
var bool = true;
for(var j = 0; j < holidays.length; j++){
if( tmpDate.getDate() == holidays[j][0] && tmpDate.getMonth() == holidays[j][1] )
bool = false;
}
if(tmpDate.getDay() != 0 && tmpDate.getDay() != 6 && bool)
days++;
}
console.log(days);
我最终计算了工作日的假期数量,然后将其与原始函数产生的结果进行差分。这具有允许我打开或关闭计算的额外优势。
var holidays=[x,y,z]
//where x,y and z are weekday numbers falling in the date range (obtained using something like .isBetween)
var desired_weekdays=[a,b]
var falling_on_weekdays= function(holidays,desired_weekdays){
for (var i=0; i < holidays.length; i++) {
if ($.inArray(holidays[i],desired_weekdays)==0){
count++}
}
}
};
然后:
if($('.#checkbox').prop("checked")===true){
return days.reduce(sum,0);
}
else {
return days.reduce(sum,0)-falling_on_weekdays;
}
从 this question 开始,它使用以下方法计算给定日期范围内特定工作日的数量:
function countCertainDays( days, d0, d1 ) {
var ndays = 1 + Math.round((d1-d0)/(24*3600*1000));
var sum = function(a,b) {
return a + Math.floor( ( ndays + (d0.getDay()+6-b) % 7 ) / 7 );
};
return days.reduce(sum,0);
}
我还想知道如果假期落在范围内的工作日,如何排除假期,假设我有一系列假期发生在 d0
和 d1
之间。
感谢帮助!
前段时间我写了类似的脚本也许对你有帮助:
var today = new Date();
var end = new Date(2016, 6, 1);
var allDays = Math.floor((end.getTime() - today.getTime())/ 86400000);
var holidays = [
[15, 8],[1, 10],[17, 10],[29,9],[30,9],[23,11],[24,11],[25,11],[26,11],[27,11],[28,11],[29,11],[30,11],[31,11],[1,0],[2,0], [3,0],[4,0],[5,0],[6,0],[7,0],[1,1],[31,0],[22,1],[23,1],[24,1],[25,1],[26,1],[24,2],[25,2],[26,2],[27,2],[28,2],[29,2],[1,4],[8,4],
];
var days = 0;
for(var i = 0; i < allDays; i++){
var tmpDate = new Date();
tmpDate.setTime(today.getTime() + i * 24*60*60*1000);
var bool = true;
for(var j = 0; j < holidays.length; j++){
if( tmpDate.getDate() == holidays[j][0] && tmpDate.getMonth() == holidays[j][1] )
bool = false;
}
if(tmpDate.getDay() != 0 && tmpDate.getDay() != 6 && bool)
days++;
}
console.log(days);
我最终计算了工作日的假期数量,然后将其与原始函数产生的结果进行差分。这具有允许我打开或关闭计算的额外优势。
var holidays=[x,y,z]
//where x,y and z are weekday numbers falling in the date range (obtained using something like .isBetween)
var desired_weekdays=[a,b]
var falling_on_weekdays= function(holidays,desired_weekdays){
for (var i=0; i < holidays.length; i++) {
if ($.inArray(holidays[i],desired_weekdays)==0){
count++}
}
}
};
然后:
if($('.#checkbox').prop("checked")===true){
return days.reduce(sum,0);
}
else {
return days.reduce(sum,0)-falling_on_weekdays;
}