Al.
Al.

Reputation: 299

jQuery DatePicker - Choose Next Weekday if Current Date Falls on Weekend

I know how to gray out the weekends on the jQuery Datepicker by instantiating it like this:

$('.calendar').datepicker({ beforeShowDay: $.datepicker.noWeekends });

The Datepicker also automatically selects the current date, so if the current date happens to be on a weekend, nothing is selected.

How can I make it select the next weekday if the currentdate falls on a weekend?

Thanks a lot!

Al

Upvotes: 0

Views: 2309

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

You can loop through and find the next valid date based on whatever beforeShowDay function you're using and set the defaultDate property to the result, like this:

var sd = new Date();
for(var i = 0; i < 7; i++) {
    if($.datepicker.noWeekends(sd)[0]) break; //found a valid day, hop out
    sd.setDate(sd.getDate() + 1); //move to the next day
}
$('.calendar').datepicker({ 
    beforeShowDay: $.datepicker.noWeekends,
    defaultDate: sd
});

Here's a demo that works for today (otherwise you could only see it work on weekends :)

Upvotes: 2

Related Questions