请有人帮我更新一下,以便能够从阵列中检测出年份。我想要做的目标是让标签不包括假日或周末。因此,当列出假期或周末时,它将显示下一个可用日期。例如,如果今天(星期四)10/15/15,标签将显示" DUE 10/16/15 @ 5:00"。但如果明天(周五)2015年10月16日,标签将显示" DUE 10/19/15 @ 5:00"。这同样适用于假期,这将是下一个可用日,而不是周末。
这就是我所拥有的:http://jsfiddle.net/byyeh83t/1/,在您考虑这一年之前似乎有效,因为数组仅按月和日进行。所以我现在需要添加一些东西来检查一年。
请有人帮我调整一下以检查正确的年份。 http://jsfiddle.net/byyeh83t/3/
如果1/18/15是星期天的日期,那么输出应该是1/20/15,因为2015年1月19日是MLK假期,但它返回1/19/15
$(document).ready(function() {
var natDays = [
[2014, 1, 1, 'New Year'],
[2014, 1, 20, 'Martin Luther King'],
[2014, 2, 17, 'Washingtons Birthday'],
[2014, 5, 26, 'Memorial Day'],
[2014, 7, 4, 'Independence Day'],
[2014, 9, 1, 'Labour Day'],
[2014, 10, 13, 'Columbus Day'],
[2014, 11, 11, 'Veterans Day'],
[2014, 11, 27, 'Thanksgiving Day'],
[2014, 11, 28, 'Thanksgiving Day'],
[2014, 12, 25, 'Christmas'],
[2014, 12, 26, 'Christmas'],
[2015, 1, 1, 'New Year'],
[2015, 1, 19, 'Martin Luther King'],
[2015, 2, 16, 'Washingtons Birthday'],
[2015, 5, 25, 'Memorial Day'],
[2015, 7, 3, 'Independence Day'],
[2015, 9, 7, 'Labour Day'],
[2015, 10, 12, 'Columbus Day'],
[2015, 11, 11, 'Veterans Day'],
[2015, 11, 26, 'Thanksgiving Day'],
[2015, 11, 27, 'Thanksgiving Day'],
[2015, 12, 24, 'Christmas'],
[2015, 12, 25, 'Christmas']
];
// dateMin is the minimum delivery date
var dateMin = new Date("1/18/2015");
dateMin.setDate(dateMin.getDate() + (dateMin.getHours() >= 14 ? 1 : 0));
function AddBusinessDays(curdate, weekDaysToAdd) {
var date = new Date(curdate.getTime());
while (weekDaysToAdd > 0) {
date.setDate(date.getDate() + 1);
//check if current day is business day
if (noWeekendsOrHolidays(date)[0]) {
weekDaysToAdd--;
}
}
return date;
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
return (noWeekend[0] ? nationalDays(date) : noWeekend);
}
function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}
function setDeliveryDate(date) {
$('#delivery-date').text($.datepicker.formatDate('mm/dd/yy', date));
}
setDeliveryDate(AddBusinessDays(dateMin, 1));
});
非常感谢任何帮助。
答案 0 :(得分:2)
你应该:
natDays[i][0]
(曾经是月份)更改为natDays[i][1]
natDays[i][1]
(曾经是当天)更改为natDays[i][2]
natDays[i][2]
(曾经是描述)更改为natDays[i][3]
nationalDays
函数中的if:date.getFullYear() == natDays[i][0]
像这样:
if (date.getFullYear() == natDays[i][0] && date.getMonth() == natDays[i][1] - 1 && date.getDate() == natDays[i][2]) {
return [false, natDays[i][3] + '_day'];
}