3

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

        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);
4

5 回答 5

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

于 2019-04-17T13:27:23.313 回答
0
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);
于 2013-11-21T07:02:27.580 回答
0

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

编辑:

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

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

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

于 2013-11-06T05:17:02.147 回答
0

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

于 2013-11-05T13:01:00.223 回答
-1

有些错误,但好主意。这是对的:

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

于 2018-09-05T13:42:38.323 回答