jQuery显示明天约会,除非明天是周末或假日

时间:2015-10-15 17:28:57

标签: jquery

我有一个名为dbo.Holidays的SQL数据库表,我正在使用ColdFusion进行查询。我正在将数据库转换为这样的JS数组:

var natDays = [
    [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, 14, 'Columbus Day'], //2013
    [11, 11, 'Veterans Day'], //2013
    [11, 28, 'Thanksgiving Day'], //2013 
    [12, 25, 'Christmas'] //2013     
    ];

我要做的目标是让标签不包括假日或周末。因此,当列出假期或周末时,它将显示下一个可用日期。例如,如果今天(星期四)10/15/15,标签将显示" DUE 10/16/15 @ 5:00"。但如果明天(周五)2015年10月16日,标签将显示" DUE 10/19/15 @ 5:00"。这同样适用于假期,这将是下一个可用日,而不是周末。

现在我测试了明天,它仍然显示星期六的日期。 http://jsfiddle.net/byyeh83t/

$(document).ready(function() {

var natDays = [
    [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, 14, 'Columbus Day'], //2013
    [11, 11, 'Veterans Day'], //2013
    [11, 28, 'Thanksgiving Day'], //2013 
    [12, 25, 'Christmas'] //2013     
    ];


// dateMin is the minimum delivery date
var dateMin = new Date("10/16/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)) {
            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));

});

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在行if (noWeekendsOrHolidays(date))中,您实际上在评估数组,而不是布尔值。检查函数noWeekendsOrHolidays是否返回其第一个位置为布尔值的数组。

您应该使用if (noWeekendsOrHolidays(date)[0])更改该内容。

编辑请注意,您还可以更改该函数,使其仅返回布尔值,因为您没有使用数组中的其他值。在这种情况下,您应该只返回第一个位置,如下所示:return (noWeekend[0] ? nationalDays(date)[0] : noWeekend[0]);。而且,如果您不在其他任何地方使用这些功能,则同样适用于nationalDays