用JavaScript计算不包括周末和节假日的天数 [英] Calculate days excluding weekend and holiday in JavaScript

查看:138
本文介绍了用JavaScript计算不包括周末和节假日的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码,该代码将计算不包括周末和自定义假期的总天数。我在stackoverflow和adobe论坛中搜索以找到解决方案,并附带以下代码。
如果公共假期在工作日(星期六至星期三)进行计算,则不包括在内。
我的问题是,如果公共假日在周末(星期四至星期五)下降,则这两个月都将扣除(假日和周末)。假设休假时间为18/09 / 2018-22 / 09/2018,则显示总天数为2天,而不是3.对于17/10 / 2018-21 / 10/2018,总显示为1天的天数则显示为3天。
任何帮助或任何想法来解决问题都将很棒!
问候

I am trying to write a code where total days will be counted excluding weekends and custom defined holiday. I searched through stackoverflow and adobe forum to find a solution and came with below code. If public holiday falls in a working day (Saturday-Wednesday) it is excluding from calculation. My problem is that if public holiday falls in weekend (Thursday-Friday), it is deducting for both (holiday & weekend). Suppose leave duration is 18/09/2018-22/09/2018, total count 2 days is showing in place of 3. Again for 17/10/2018-21/10/2018, total count 1 day is showing in place of 3 days. Any help or any idea to solve the problem would be great! Regards

//Thursday and Friday will be excluded as weekend.
var start = this.getField("From").value;
// get the start date value
var end = this.getField("To").value;
var end = util.scand("dd/mm/yyyy H:MM:SS", end + " 0:00:00");
var start =util.scand("dd/mm/yyyy H:MM:SS", start + " 0:00:00");
event.value = dateDifference(start, end);
function dateDifference(start, end) {
  // Copy date objects so don't modify originals
  var s = new Date(+start);
  var e = new Date(+end);
  // Set time to midday to avoid daylight saving and browser quirks
  s.setHours(12,0,0,0);
  e.setHours(12,0,0,0);
  // Get the difference in whole days
  var totalDays = Math.round((e - s) / 8.64e7);
  // Get the difference in whole weeks
  var wholeWeeks = totalDays / 7 | 0;
  // Estimate business days as number of whole weeks * 5
  var days = wholeWeeks * 5;
  // If not even number of weeks, calc remaining weekend days
  if (totalDays % 7) {
    s.setDate(s.getDate() + wholeWeeks * 7);
    while (s < e) {
      s.setDate(s.getDate() + 1);
      // If day isn't a Thursday or Friday, add to business days
      if (s.getDay() != 4 && s.getDay() != 5) {
        ++days;
      }
    }
  }
var hdayar = ["2018/02/21","2018/03/17","2018/03/26","2018/04/14","2018/05/01","2018/08/15","2018/09/2 1","2018/10/18","2018/10/19","2018/12/16","2018/12/25"];
//test for public holidays
var phdays = 0;
for (var i = 0; i <hdayar.length; i++){
if ((Date.parse(hdayar[i]) >= Date.parse(start)) && (Date.parse(hdayar[i]) <= Date.parse(end))) {phdays ++;}}
  return days-phdays + 1;
}


推荐答案

您应该使用库

但是如果您想自己做,可以使用 .getDay 进行检查。

But if you want to do it yourself you could use .getDay to check if the public holidays are on a weekend.

var weekend = [4, 5],   // for Thursday, Friday
    holDate, holDay;
for (var i = 0; i < hdayar.length; i++){
    holDate = Date.parse(hdayar[i]);
    holDay = new Date(holDate).getDay()
    if (weekend.indexOf(holDay) == -1 && holDate >= Date.parse(start) && holDate <= Date.parse(end)) {
        phdays ++;
    }
}

phdays 现在将包含该范围内的非周末公共假日的数量。

phdays will now contain the number of non-weekend public holidays within the range.

这篇关于用JavaScript计算不包括周末和节假日的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆