【问题标题】:Calculate days excluding weekend and holiday in JavaScript在 JavaScript 中计算不包括周末和节假日的天数
【发布时间】:2018-04-21 17:18:51
【问题描述】:

我正在尝试编写一个代码,其中将计算除周末和自定义假期之外的总天数。我通过 stackoverflow 和 adobe 论坛搜索以找到解决方案并附带以下代码。 如果公共假期在工作日(周六至周三),则不计算在内。 我的问题是,如果公共假期是在周末(周四至周五),它会同时扣除(假期和周末)。假设休假持续时间为 18/09/2018-22/09/2018,总计数 2 天代替 3。再次为 17/10/2018-21/10/2018,总计数 1 天代替3天。 任何帮助或解决问题的任何想法都会很棒! 问候

//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;
}

【问题讨论】:

  • 周四到周五是周末?
  • 是的,周四到周五是周末(习惯了)
  • @Luca——在中东很常见。
  • 您的代码似乎基于here 的答案,您应该提及这一点。此外,您不应使用内置解析器,请参阅Why does Date.parse give incorrect results?。时间不需要设置两次,你一开始好像设置为 00:00:00 就够了。无需将其设置为 12:00:00。

标签: javascript date


【解决方案1】:

您应该为此使用库而不是重新发明轮子。

但如果你想自己做,你可以使用.getDay 来检查公共假期是否在周末。

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 现在将包含该范围内的非周末公共假期的数量。

【讨论】:

  • 嗨,当开始和结束字段中没有输入时,我在计算字段中总是得到 1。如果我从 return days-phdays + 1 中删除 1 ,那么当开始日和结束日相同时,结果显示为零而不是 1 天。任何想法
  • 没有输入时你想要什么响应?只是将其作为错误或特殊情况处理?
【解决方案2】:

只是有相同的要求,这是我的工作。希望它可以帮助其他人

var holiday = ["4/18/2019", "4/19/2019", "4/20/2019", "4/25/2019", "4/26/2019"];
var startDate = new Date();
var endDate = new Date(startDate.setDate(startDate.getDate() + 1));

for (i = 0; i < holiday.length; i++) {
    var date = endDate.getDate();
    var month = endDate.getMonth() + 1; //Months are zero based
    var year = endDate.getFullYear();
    if ((month + '/' + date + '/' + year) === (holiday[i])) {
        endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        if (endDate.getDay() == 6) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 2));
        } else if (endDate.getDay() == 0) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        }
    }
}

在这里,结束日期为您提供下一个工作日。在这里,我忽略当前日期并从第二天开始比较是假期还是周末。您可以根据您的要求自定义 dateTime (month + '/' + date + '/' + year)。比较两个时要小心彼此约会。因为它看起来一样,但实际上不是。所以相应地定制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多