1
我正在嘗試做2件事。在某一個月基於當前日的工作日內最新用Javascript計算幾個工作日
- 計算數(即今天是2012年3月7日,因此,5個工作日過去了)在給定的去的工作日
- 計算數一個月基於當前日(即今天是2012年3月7日,因此,也有這個月剩下17個工作日
這裏任何幫助,將不勝感激
編輯:。 這是我」至今嘗試過:
function isWeekday(year, month, day) {var day = new Date(year, month, day).getDay();return day !=0 && day !=6;}
function getWeekdaysInMonth(month, year) {var days = daysInMonth(month, year);var weekdays = 0;for(var i=0; i< days; i++) {if (isWeekday(year, month, i+1)) weekdays++;}return weekdays;}
function calcBusinessDays(dDate1, dDate2) {
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
}
不太清楚如何把它放在一起,以獲得工作日和工作日。
您到目前爲止嘗試過了哪些?你試圖解決的具體問題是否給你帶來麻煩? – maerics 2012-03-08 01:26:50
你是否也想考慮假期呢? – 2012-03-08 01:27:01
Maerics我已經更新了我的要求。雅各布,假期並不重要。 – st4ck0v3rfl0w 2012-03-08 01:34:53