2014-02-24 86 views
1

我有兩個日期。一個是開始日期,另一個是結束日期。我想計算日期範圍內有多少星期六,星期一和星期三?我該如何解決它?我看了幾個教程,但他們只計算日期範圍內的日期。提前致謝。我使用下面的代碼來計算只有工作日,但我只需要在日期範圍內有多少星期六,星期一和星期三。Javascript日期範圍內有多少個特定日期

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=utf-8 /> 
<title>JS Bin</title> 
<!--[if IE]> 
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> 
<![endif]--> 
    <script> 
     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 
    } 
    </script> 
<style> 
    article, aside, figure, footer, header, hgroup, 
    menu, nav, section { display: block; } 
</style> 
</head> 
<body> 
    <script> 
    alert(calcBusinessDays(new Date("August 01, 2010 11:13:00"),new Date("August 31, 2010 11:13:00"))); 
    </script> 
</body> 
</html> 
+0

你嘗試過什麼嗎? –

+0

請提供您嘗試展示一些功能的示例。我們在這裏幫助您,而不是爲您編寫所有代碼。謝謝 –

+0

如果你已經有單日期的範圍,你可以迭代它們並篩選出你感興趣的所有工作日。爲了得到一週的某一天,你可以使用[Date.getDay()](http ://www.w3schools.com/jsref/jsref_getday.asp) – jhohlfeld

回答

2

我的方法:

首先,從範圍內獲得兩個日期之間的日期列表:

function getDates(dateStart, dateEnd) { 
    var currentDate = dateStart, 
     dates = []; 
    while(currentDate <= dateEnd) { 

    // append date to array 
    dates.push(currentDate); 

    // add one day 
    // automatically rolling over to next month 
    var d = new Date(currentDate.valueOf()); 
    d.setDate(d.getDate() + 1); 
    currentDate = d; 

    } 
    return dates; 
} 

然後循環通過這些日期,篩選了相關工作一天指數:

function filterWeekDays(dates, includeDays) { 
    var weekdays = []; 

    // cycle dates 
    dates.forEach(function(day){ 

    // cycle days to be included (so==0, mo==1, etc.) 
    includeDays.forEach(function(include) { 
     if(day.getDay() == include) { 
     weekdays.push(day); 
     } 
    }); 
    }); 
    return weekdays; 
} 

過濾選項:

星期日beeing 0和週一是1,週日,週一的名單,週三將是:[0, 1, 3]

JS斌:http://jsbin.com/furupoqu/5/edit

+0

非常感謝。我檢查了代碼。它的工作正常。我認爲只有一個問題。它不包括最後的日期。例如,如果結束日期在工作日內,則結果會比實際結果少返回1天。在2014年1月1日至2014年1月31日,如果工作日是星期六,星期一和星期三,那麼結果將返回13個工作日,而如果我將2014年1月29日作爲我的結束日期,也是星期三,則返回12個工作日,這應該是13。 – user3150647

+0

我改變了答案。看看while語句。它現在應該包含endDate。 – jhohlfeld

+0

這非常有用。是否有任何公式可以給出一組日期,這些日期將排除在兩個日期之間或不計算在內? – Poonam

3

O(1)解決方案。一週中的幾天(不超過7天),而不是日期範圍。

// days is an array of weekdays: 0 is Sunday, ..., 6 is Saturday 
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); 
} 

例如,計數星期一(1),三(3)和星期六(6)中的2014年1月:

countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,1)) // 1 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,2)) // 1 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,3)) // 1 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,4)) // 2 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,5)) // 2 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,6)) // 3 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,7)) // 3 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,8)) // 4 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,9)) // 4 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,10)) // 4 
countCertainDays([1,3,6],new Date(2014,0,1),new Date(2014,0,11)) // 5 

注意:這假定d0d1Date對象,它們的時間的一天都差不多。如果僅創建年份,月份和日期來創建Date對象,則沒有問題。