다음 두 날짜 사이의 주말 및 공휴일 수를 계산해야합니다.주말, 휴일없이 두 날짜 사이의 공휴일 계산
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")];
다음 두 날짜 사이의 주말 및 공휴일 수를 계산해야합니다.주말, 휴일없이 두 날짜 사이의 공휴일 계산
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")];
// 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);
이것은 'countWeekEndDays'에 대해 정수가 아닌 값을 돌려 줄 수 있습니다. 예 :'Date ("2013/12/29")'~'Date ("2014/01/21")'. –
죄송합니다 .. 나는 내 대답을 편집하고 Math.floor를 추가했습니다. 감사! – gtournie
이 부분 주를 순환하므로 최대 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);
이 수치는 토요일과 일요일을 의미하며 휴일은 아닙니다. 나는 다른 출처에서 얻은 휴일 날짜 모음을 반복하지 않고도 휴일을 할 수 있다고 생각하지 않습니다.
갖는 다음과 같은 두 가지 기능 :
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);
을 그리고 시작과 endDate가 중간에 휴일을 계산 :
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
}
당신은 다음과 같이 총 weekenddays을 계산할 수 있습니다
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++;
}
추가 정보 calcBusinessDays는이 [페이지] (http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days)의 기능입니다! 나는 그것을 직접 쓰지 않았다! –
시작일이 "일요일"이고 끝이 "금요일"이면 실패합니다. 하루 만 계산됩니다. 예를 들어, 2015 년 1 월 18 일에서 2015 년 1 월 23 일까지. [여기 버그의 재생산] (http://jsfiddle.net/e6gmkLwg/) – Alvaro
그래서 무슨 일이있어 당신의 문제? –
Momentjs (http://momentjs.com/) 또는 Sugarjs (http://sugarjs.com/dates) – Jason
을 반복하지 않고 확인하십시오. startDate에서 endDate까지 반복하지 않는다는 것을 의미합니까? –