2013-03-23 103 views
0

我已經得到了我的劇本,從開始到結束日期來計算,但我越這樣做,我明白了要計算的更多...高級日期計算範圍不包括週末和節假日

公司我我寫這是因爲有一個「請求關閉」頁面。兩個文本框允許客戶以mm/dd/yyyy格式輸入開始和結束日期。我目前正在調用dateRange()函數onchange。這將處理這兩個日期,然後更新SPAN中的打印計數。

現在的挑戰。在這個範圍內,我必須排除週末,這是我見過的例子,但無法理解。我還必須排除該公司考慮帶薪假期的情況,如果他們在範圍之內,但在週末,也必須避免。

JavaScript函數:

function dateRange() { 
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds 
    var firstDate = new Date(document.getElementById('start').value); 
    var secondDate = new Date(document.getElementById('end').value); 
    var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)-1)); 
    // holDays = ?????; 
    // vacDays = diffDays - holDays; 
    document.getElementById("holDays").innerHTML = diffDays || 0; 
    document.getElementById("vacDays").innerHTML = diffDays || 0; 
} 

HTML日期輸入:

From: <INPUT type="text" name="start" id="start" value="" onchange="dateRange()" class="datepicker" /> 
To: <INPUT type="text" name="end" id="end" value="" onchange="dateRange()" class="datepicker" /> 

位置持有剩餘計數:

Paid Off Days: <SPAN id="holDays">0</SPAN> of the days in your range are Paid Holidays and will not be charged Vacation Days. 
Vacation Days: You will be using <SPAN id="vacDays">0</SPAN> Day(s) of vacation. 

因此,需要初始計,不包括週末。 在此範圍內的假日數,而不是在週末。 原始計數 - 假日數=休假天

+0

你已經包括PHP 。除非您認爲PHP很重要,否則請顯示最終的HTML – 2013-03-23 17:03:19

回答

1

好吧,我沒有迴應此,而是提供我的結果完成這樣一個樣本集度假日期的...

function dateCount() { 
    var startDate = new Date(document.getElementById('start').value); 
    var endDate = new Date(document.getElementById('end').value); 
    var holiDates = []; 
    holiDates[0] = new Date("01/16/2013"); 
    holiDates[1] = new Date("04/20/2013"); 
    holiDates[2] = new Date("01/16/2014"); 
    var length = holiDates.length, fullCount = 0, paidCount = 0, vacDays = 0; 
    for (var i = 0; i < length; i++) { 
    holiday = holiDates[i].getDay(); 
    if(holiDates[i] >= startDate && holiDates[i] <= endDate && holiday != 0 && holiday != 6) { 
     paidCount++; 
    } 
    } 
    while (startDate <= endDate) { 
    var day = startDate.getDay(); 
    if(day != 0 && day != 6) { 
     fullCount++; 
    } 
    startDate.setDate(startDate.getDate() + 1); 
    } 
    document.getElementById("holDays").innerHTML = paidCount || 0; 
    vacDays = fullCount - paidCount; 
    document.getElementById("vacDays").innerHTML = vacDays || 0; 
} 
相關問題