Neil Smart
Neil Smart

Reputation: 47

Jquery UI Datepicker - Disable the first monday after a weekend if the current day is Saturday or Sunday

I have a datepicker and I would like to disable the monday following the weekend if it is Saturday or Sunday....is this possible?

Im guessing I have to find a way to:

  1. Find the current date (day)
  2. If its a Sat or a Sun then use conditional statement to disable the following Monday. Maybe the easiest way would be to have two conditionals. So if its Sat then disable following two days and if its Sun then disable 1 day after???

Any help much appreciated on this

Upvotes: 2

Views: 1232

Answers (1)

MLM
MLM

Reputation: 3688

You can find the current date by making a new date object:

var today = new Date();

Then you can use beforeShowDay function of datepicker to disable the following monday as you described with conditionals:

Here is a working fiddle I made: http://jsfiddle.net/MadLittleMods/DfAYY/

var today = new Date(); // Go ahead and change the current day (year, month, day)

// If today is Saturday or Sunday then no Monday for you
function noFollowingMonday(thisDate) {
    // Sunday = 0, Saturday = 6;
    if(today.getDay() == 0 || today.getDay() == 6)
    {
        // Monday = 1
        // Only Mondays - that are after today - and is the first monday after today
        if (thisDate.getDay() == 1 && today.getDate() < thisDate.getDate() && Math.abs(today.getDate() - thisDate.getDate()) < 7)
        {
            return [false, "", "Unavailable"];
        }
    }

    return [true, "", "Choose"];

}

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

Upvotes: 1

Related Questions