【问题标题】:Calculate future date excluding holidays and weekends计算不包括节假日和周末的未来日期
【发布时间】:2013-11-05 11:44:23
【问题描述】:

根据问题,我想根据给定的天数找到未来的日期。它应该排除存储为数组的周末和节假日。下面有这段代码,但不起作用。

        var holiday = [];
        holiday[0] = new Date(2013, 11, 12);
        holiday[1] = new Date(2013, 11, 13);

        var startDate = new Date();
        var endDate = "", noOfDaysToAdd = 13, count = 0;
        while (count < noOfDaysToAdd) {
            endDate = new Date(startDate.setDate(startDate.getDate() + 1));
            if (endDate.getDay() != 0 && endDate.getDay() != 6) {
                // Date.getDay() gives weekday starting from 0(Sunday) to
                // 6(Saturday)
                for ( var i = 0; i < holiday.length; i++) {
                    if (endDate != holiday[i]) { //If days are not holidays
                        count++;
                    }
                }
            }
        }
        alert(endDate);

【问题讨论】:

  • 什么是不起作用
  • 它给了我错误的日期。如果我删除了 for 循环来检查假期,它可以正常工作。
  • 与您的问题相切,比较 Date 对象的相等性可能不是您想要的。

标签: javascript arrays date


【解决方案1】:

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

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)。比较两个时要小心彼此约会。因为它看起来一样但实际上不是。所以相应地定制。它计算未来的日期,不包括假期和周末

【讨论】:

    【解决方案2】:

    在您的情况下,for 循环每次运行两次,并且每次运行并且第二天不在您的预定义数组中,它会将计数加 1,从而在 7 个工作日后完成例程并为您提供日期错误。添加更多假期,您会错过更多。

    【讨论】:

      【解决方案3】:

      运行循环并计算编号。开始和结束日期之间的假期。将此计数添加到 noOfDaysToAdd,然后将此值添加到开始日期以获得最终日期。

      编辑:

      你需要从你的逻辑中纠正两件事:

      1) 日期比较不正确,当你创建一个假期日期时,你只是传递了日期、月份和年份。当您执行 new Date() 时,您也会得到一个带时间的日期。尝试提醒这两个日期并查看差异。由于这种差异,日期比较总是不相等的。

      2) 另一个问题是您在 for 循环中添加计数值。因此,对于每个日期,计数值都会增加编号。您在假期数组中拥有的假期。您也需要更正此问题。

      【讨论】:

      • 我是否在 while 循环内运行循环?
      • 请在上面的答案中查看我的编辑。
      【解决方案4】:
      var holiday = [];
          holiday[0] = new Date(2013, 10, 12);//remember that month is 0 to 11
          holiday[1] = new Date(2013, 10, 13);//remember that month is 0 to 11
          var startDate = new Date();
          var endDate = new Date(), noOfDaysToAdd = 13, count = 0;
          while (count < noOfDaysToAdd) {
              endDate.setDate(endDate.getDate()+1)
              // Date.getDay() gives weekday starting from 0(Sunday) to
              // 6(Saturday)
              if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate,   holiday)) {
                  count++;
              }
          }
      function isHoliday(dt, arr){
      var bln = false;
      for ( var i = 0; i < arr.length; i++) {
          if (compare(dt, arr[i])) { //If days are not holidays
              bln = true;
              break;
          }
      }
      return bln;
      }
      function compare(dt1, dt2){
      var equal = false;
      if(dt1.getDate() == dt2.getDate() && dt1.getMonth() == dt2.getMonth() && dt1.getFullYear() == dt2.getFullYear()) {
          equal = true;
      }
      return equal;
      }
          alert(endDate);
      

      【讨论】:

        【解决方案5】:

        一些错误,但好主意。这是正确的:

        var holiday = [];
            holiday[0] = new Date(2018, 10, 01);//remember that month is 0 to 11
            holiday[1] = new Date(2018, 10, 11);//remember that month is 0 to 11
        	holiday[2] = new Date(2018, 11, 25);//remember that month is 0 to 11
        	holiday[3] = new Date(2018, 11, 26);//remember that month is 0 to 11
        	holiday[4] = new Date(2019, 00, 01);//remember that month is 0 to 11
        	
        	var a = Date.parse(document.getElementById(1).value);
        	var b = Date.parse(document.getElementById(2).value);
        	
            var startDate = new Date(a);
            var endDate = new Date(b);
        	//var noOfDaysToAdd = 8;
        	var count = 0;
        	var czydata = false;
            
        	if (startDate > endDate)
        	{
        		alert("POPRAW DANE!!! Data OD nie moze byc wieksz od Daty DO");
        	}
        	else
        	{
        		while (czydata == false) {
        			czydata = cmpday(startDate,endDate)
        			 // Date.getDay() gives weekday starting from 0(Sunday) to
        			// 6(Saturday)
        			if (startDate.getDay() != 0 && startDate.getDay() != 6 && !isHoliday(startDate,holiday)) {
        				
        				count++;
        				
        			}
        			startDate.setDate(startDate.getDate()+1);
        		}
        	}
        	function isHoliday(dt, arr){
        	var bln = false;
        	for ( var i = 0; i < arr.length; i++) {
        		if (compare(dt, arr[i])) { //If days are not holidays
        			bln = true;
        			break;
        		}
        	}
        	return bln;
        	}
        	
        	function compare(dt1, dt2){
        	var equal = false;
        	if(dt1.getDate() == dt2.getDate() && dt1.getMonth() == dt2.getMonth() && dt1.getFullYear() == dt2.getFullYear()) {
        		equal = true;
        	}
        	return equal;
        	
        	}
        	
        	function cmpday(date1, date2){
        	var eqdate = false;
        	if(date1.getDate() == date2.getDate() && date1.getMonth() == date2.getMonth() && date1.getFullYear() == date2.getFullYear()) {
        		eqdate = true;
        	}
        	return eqdate;
        	
        	}

        【讨论】:

        • 哦@jarek 我欠你道歉,因为我错误地否决了你的回答
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多