3

I am using the jquery datepicker with a beforeShowDay filter that only enables Tuesdays. If I'm near the end of the month and there are no more Tuesdays, the picker opens to the current month with no dates available.

How do I recognize this situation with the picker? Is there a way to ask for available dates for the selected month or something like that?

If I do recognize this situation, how do I get the picker to open on the next month when the user clicks?

Is there an automatic way to do this?

lincolnk
  • 11,218
  • 4
  • 40
  • 61

3 Answers3

3

Try this jsfiddle I made.

$(document).ready( function() {
    $('#datepicker').datepicker({
        defaultDate: nextTuesday(),
        beforeShowDay: enableTuesdays
    });
});

function enableTuesdays(date) {
    // 0 =s unday, 1 = monday, 2 = tuesday, 3 = wednesday,
        // 4 = thursday, 5 = friday, 6 = saturday
    var day = date.getDay();
    return [(day == 2), ''];
}

function nextTuesday() {
    var today = new Date();
    var offset = (today.getDay() < 2) ? 0 : 7;
    var daysUntilTuesday = 2 + offset - today.getDay();
    return '+'+daysUntilTuesday;
}
Ben
  • 764
  • 4
  • 19
0

Use the datepicker setDate() method - http://jqueryui.com/demos/datepicker/#method-setDate - to set the calendar date to the next available Tuesday in the beforeShowDay event.

There is an example for getting the next specific day of the week here: Get date of specific day of the week in JavaScript

Community
  • 1
  • 1
benedict_w
  • 3,543
  • 1
  • 32
  • 49
0

While this doesn't address figuring out the logic behind getting the calendar to open with the next month if there are no more available dates. If the front-end design allows for it you could have the datepicker open up with a 2 month view, that way you would always have dates to choose?

Setup would require that you pass in 'numberOfMonths' as an option on initialization of the datepicker.

$('#datepicker').datepicker({ numberOfMonths: 2 });
aztechy
  • 640
  • 7
  • 15