【问题标题】:Exclude holidays between 2 Dates不包括 2 个日期之间的假期
【发布时间】:2017-06-13 08:27:40
【问题描述】:

我在网上找遍了,我终于把这段代码放在了 Weekend Exclusion.

function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
    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
  }

我不知道如何添加代码以排除假期。我知道我必须创建一个数组来存储假期,但在那之后我坚持了下来。我需要你的帮助,非常感谢。

【问题讨论】:

  • 你想单独排除周末或假期吗?
  • 我想排除周末和节假日。
  • 在这种情况下,您必须知道假期的日期。你有这些信息吗?
  • 只是说(2017-06-25、2017-06-26、2017-06-27)作为假期。
  • 知道了...检查一下。

标签: javascript html


【解决方案1】:

只需遍历假期数组并检查日期是否在该范围内,并检查假期是在星期六还是星期日,在这种情况下它已经被计算在内。下面是修改后的例子。

function calcBusinessDays(dDate1, dDate2, holidays) { // input given as Date objects
    var iWeeks, iDateDiff, iAdjust = 0, i;
    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

    for(i = 0; i < holidays.length; i++) {
      if(holidays[i] >= dDate1 && holidays[i] <= dDate2 && holidays[i].getDay() != 0 && holidays[i].getDay() != 6) {
        iDateDiff--;
      }
    }

    return (iDateDiff + 1); // add 1 because dates are inclusive
  }

var holidays = [ new Date(2017, 5, 2), new Date(2017, 5, 3), new Date(2017, 5, 4), new Date(2017, 5, 5) ];

calcBusinessDays(new Date(2017,1,1), new Date(), holidays);
// returns 93

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 2017-01-28
    • 1970-01-01
    • 2011-04-06
    • 2019-06-25
    • 2015-04-25
    相关资源
    最近更新 更多