我已经让我的脚本从开始到结束日期进行计算,但是我做的越多,我就越意识到需要计算的内容......
我写这篇文章的公司有一个“请求关闭”页面。两个文本框允许客户端以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 :(得分: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;
}