计算两个日期之间的工作日和周末而不考虑夏令时-JavaScript

时间:2019-11-12 11:43:02

标签: javascript jquery

由于伦敦的夏时制,我无法获得平日和周末的正确计数。在下面的小提琴中,2019年10月26日考虑的时间有时是工作日,有时是周末。 我想得到以下小提琴的输出是 “ workingDays”:1   “ weekendDays”:2

有什么建议吗?

var startDate = new Date('2019-10-26');
var endDate = new Date('2019-10-28');
var workingDays = number_of_working_days(startDate, endDate);
var weekendDays = number_of_weekend_days(startDate, endDate);
console.log({
  'workingDays': workingDays,
  'weekendDays': weekendDays
})

function number_of_working_days(startDate, endDate) {
  var workingDays = 0;
  for (var i = startDate; i <= endDate; i = i + (60 * 60 * 24)) {
    if (i.getDay() <= 5) {
      workingDays = workingDays + 1;
    }

  }
  return workingDays;
}

function number_of_weekend_days(startDate, endDate) {
  var weekendDays = 0;
  for (var i = startDate; i <= endDate; i = i + (60 * 60 * 24)) {
    if (i.getDay() > 5) {
      weekendDays = weekendDays + 1;
    }
  }
  return weekendDays;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

0 个答案:

没有答案