我必须计算以下两个日期之间的周末计数和假日数。
var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var holidays = [new Date("01/06/2014"), new Date("01/26/2014")];
答案 0 :(得分:2)
这循环一个部分周,因此最多有6次迭代。我想不出用纯JS来解决这个问题的更优雅的方法。
var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var diff = Math.abs(startDate - endDate); // in milliseconds
var ms_per_day = 1000*60*60*24;
var days = diff/ms_per_day + 1; // convert to days and add 1 for inclusive date range
var mod = days % 7;
var full_weeks = (days - mod) / 7;
var weekend_days = full_weeks * 2;
if (mod != 0) { // iterate through remainder days
var startPartialWeek = new Date();
var endPartialWeek = endDate;
startPartialWeek.setTime(endDate.getTime() - (mod - 1)*ms_per_day);
for (var d = startPartialWeek; d <= endPartialWeek; d.setDate(d.getDate() + 1)) {
if(d.getDay() == 0 || d.getDay() == 6) {
weekend_days++;
}
}
}
alert(weekend_days);
这只计算周六和周日,而不是假期。我不认为你可以在没有迭代从其他来源获得的假日日期的集合中度假。
答案 1 :(得分:1)
具有以下两个功能:
function calculateTotalDays(firstDate, secondDate){
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
return diffDays;
}
function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1) return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
您可以按如下方式计算总周末日期:
var startDate = new Date("01/02/2014");
var endDate = new Date("02/06/2014");
var totalDays = calculateTotalDays(startDate, endDate);
var weekendDays = totalDays - calcBusinessDays(startDate, endDate);
然后在start和endDate之间计算假期:
var totalHolidays = 0;
for (var i = 0, i < holidays.length; i++){
var d = holidays[i].getDay();//Make sure holiday is not a weekendday!
if (holidays[i] >= startDate && holidays[i] <= endDate && !(d == 0 || d==6))
totalHolidays++;
}
答案 2 :(得分:0)
// Count days between the 2 dates
var days = Math.floor(((Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())
- Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) / (24 * 60 * 60 * 1000)));
// Count holidays
var countHolidays = 0;
for (var i = 0, len = holidays.length; i < len; ++i)
if (holidays[i] >= startDate && holidays[i] <= endDate)
++countHolidays;
// Vars used to count sundays and saturdays
var adjustingDays1 = (7 - startDate.getDay()) % 7, // days between week of startDate and sunday
adjustingDays2 = (7 + 6 - startDate.getDay()) % 7, // days between week of startDate and saturday
oddDays = days % 7; // remainder of total days after dividing 7
completeWeeks = Math.floor(days / 7);
// Count weekend days
var countWeekEndDays = completeWeeks + (oddDays >= adjustingDays1 ? 1 : 0) +
completeWeeks + (oddDays >= adjustingDays2 ? 1 : 0);
console.log('holidays: ' + countHolidays);
console.log('weekend days: ' + countWeekEndDays);