1

我已经让我的脚本从开始到结束日期进行计算,但是我这样做的次数越多,我就越意识到必须计算什么......

我写这篇文章的公司有一个“请求关闭”页面。两个文本框允许客户端以 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.

所以,需要原始计数,不包括周末。该范围内的假期天数,而不是周末。原始计数 - 假期计数 = 假期天数

4

1 回答 1

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;
}
于 2013-04-07T16:37:11.207 回答