bobo2000
bobo2000

Reputation: 1877

datepicker not populating dates properly

Here is my jquery code:

   var unavailableDates = ["28-06-2011","22-12-2011","22-10-2011","22-02-2012",
                           "21-10-2011","21-07-2011","19-10-2011","18-11-2011",
                           "18-10-2011","18-05-2011","18-04-2011","17-12-2010",
                           "17-10-2011","14-10-2011","13-02-2012","12-11-2011",
                           "12-04-2011","09-12-2011","09-05-2011","09-02-2012",
                           "07-11-2011","07-03-2012","05-07-2011","03-05-2011",
                           "02-12-2011","01-11-2011","01-08-2011","01-06-2011"];


function unavailable(date) {
    dmy = date.getDate() + "-"+ (date.getMonth() + 1) +"-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == -1) {
        return [false, "Unavailable"];
    } else {
        return [true, "", ""];
    }
}

$(function() {
$("#iDate").datepicker({
    dateFormat: 'yy-mm-dd',
    beforeShowDay: unavailable,
    onSelect: function (dateText, inst) {
        $('#frmDate').submit();
     }
  });
});

For some reason it is populating some of the datepicker controller with some dates from the array, but omitting others. I have no idea why it is behaving this way.

Can somebody help?

Thank you.

Upvotes: 0

Views: 157

Answers (1)

Martin
Martin

Reputation: 2230

unavailable() contains faulty logic. When getDate() and getMonth() return single digit number, you try to find date that does not exist in you array. For example you search "1-8-2011" instead of "01-08-2011". One solution is to left pad your date parts with zeroes when necessary.

JsFiddle example.

Upvotes: 1

Related Questions