Vicki
Vicki

Reputation: 1456

jQuery display Tomorrows date unless tomorrow is a weekend or holiday

I have a SQL database table named dbo.Holidays that I am querying using ColdFusion. I am converting the database into an JS array like this:

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

The goal I am trying to do is have the label not include the holiday days or weekends. So when a holiday is listed or its the weekend it will display the next available day. For example if today (thursday) 10/15/15, the label will show "DUE 10/16/15 @ 5:00". But if tomorrow (friday) 10/16/15, the label will show "DUE 10/19/15 @ 5:00". And the same would apply for holidays it would be the next available day and not on the weekend.

Right now I tested for tomorrow and its still showing Saturdays date. 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));

});

Any help with this would be greatly appreciated!

Upvotes: 0

Views: 220

Answers (1)

Diego
Diego

Reputation: 16724

In the line if (noWeekendsOrHolidays(date)) you are actually evaluating an array, not a boolean. Check that the function noWeekendsOrHolidays returns an array where its first position is a boolean.

You should change that with if (noWeekendsOrHolidays(date)[0]).

Edit Note that you could also change that function so that it returns only the boolean because you are not using the other values in the array. In that case you should return only the first position, like this: return (noWeekend[0] ? nationalDays(date)[0] : noWeekend[0]);. And, if you don't use the functions anywhere else, the same applies to nationalDays.

Upvotes: 1

Related Questions