计算未来的日期,不包括假日和周末

时间:2013-11-05 11:44:23

标签: javascript arrays date

根据问题,我希望根据给定的天数找到未来的日期。它应该排除存储为数组的周末和假日。请在下方输入此代码但不起作用。

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

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

修改

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

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

2)另一个问题是你在循环中添加计数值。因此,对于每个日期,计数值将增加no。你假期阵列中的假期。你也需要纠正这个问题。

答案 2 :(得分: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);

答案 3 :(得分:0)

有同样的要求,这就是我的解决方法。希望对其他人有帮助

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

在这里,结束日期为您提供下一个工作日。在这里,我忽略了当前日期,并从第二天开始进行比较,无论是假日还是周末。您都可以根据自己的需求(month + '/' + date + '/' + year)自定义dateTime。比较两个日期。因为它看起来一样,但实际上却不一样。因此要进行相应的自定义。它会计算出不包括假期和周末的将来日期

答案 4 :(得分:-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;
	
	}