2014-01-28 125 views
1

我必須計算週末計數和假期計數在以下兩個日期之間。週末,假期計數兩個日期之間沒有迭代

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")]; 
+3

那麼,什麼是你的題? –

+1

檢查出Momentjs(http://momentjs.com/)或Sugarjs(http://sugarjs.com/dates) – Jason

+0

沒有迭代,你是不是從startDate迭代到endDate? –

回答

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); 
+0

這可以返回'countWeekEndDays'的非整數值。例如:'Date(「2013/12/29」)'到'Date(「2014/01/21」)''。 –

+0

糟糕..我編輯了我的答案並添加了一個Math.floor。謝謝! – gtournie

1

這遍歷一個完整的星期,所以最多有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

有以下兩個功能:

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 startDate = new Date("01/02/2014"); 
var endDate = new Date("02/06/2014"); 
var totalDays = calculateTotalDays(startDate, endDate); 
var weekendDays = totalDays - calcBusinessDays(startDate, 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++; 
} 
+0

作爲附加信息calcBusinessDays是從這個[頁面]的功能(http://stackoverflow.com/questions/3464268/find-day-difference-between-two-dates-excluding-weekend-days)!我沒有自己寫! –

+0

如果開始日是「星期日」,最後一個是「星期五」,則此操作失敗。它只計算1天。例如,從2015年1月18日到2015年1月23日。[這是錯誤的再現](http://jsfiddle.net/e6gmkLwg/) – Alvaro

相關問題