Louis
Louis

Reputation: 221

Jquery UI date Picker disable enable Sunday, Thursday and disable specific dates

I'm trying to enable only Thursdays and Sundays but also disable some specific Sundays or Thursdays. I'm trying with this function but it's not working yet:

<script>
    var unavailableDates = ["2013-03-31", "2013-03-24"];

    function disabledays(date) {

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

        //Show only sundays and thuersdays
        var day = date.getDay();
        return [(day == 0 || day == 4)];

    }

$('#txtDate').datepicker({
    beforeShowDay: disabledays

})

</script>

Upvotes: 0

Views: 1970

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126082

Two problems:

  1. The code that builds a date string does not add a 0 to the month portion. You could change your unavailableDates array.

  2. You need to check the return value of $.indexOf to see if it's >= 0 instead of just equal to zero.

With both changes:

var unavailableDates = ["2013-3-31", "2013-3-24"];

function disabledays(date) {
    var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    if ($.inArray(ymd, unavailableDates) >= 0) {
        return [false, "", "Unavailable"];
    } else {
        //Show only sundays and thuersdays
        var day = date.getDay();
        return [(day == 0 || day == 4)];
    }
}

$('#txtDate').datepicker({
    beforeShowDay: disabledays
});

Example: http://jsfiddle.net/XJKbV/

Upvotes: 1

Related Questions