【发布时间】: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