Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm following behind another coder and have never used jQuery's Datepicker before, and I am somewhat lost... and not very good at this in the first place. I've been asked to disable all Sundays in Datepicker so people can't select those days for appointments, but all the original coder left me was:
JavaScript
jQuery('#date-needed').datepicker({
    minDate: 0,
    maxDate: '+14d'          
});

I know this means that people can't make appointments outside of two weeks from the day they make them, but that's about it. There must be a simple(ish) way to solve this, but nothing I've tried has blacked out the Sundays, and most methods seem to break the widget altogether. Help!

What I have tried:

I've tried simple things, like adding
JavaScript
daysOfWeekDisabled: [0]

and...
JavaScript
WeekdaysDisabled: [0]

and I got a little fancier and tried to make a function...
JavaScript
JQuery('#date-needed').datepicker({
    minDate: 0,
    maxDate: '+14d'
});

JQuery('#date-needed').ready(function() {
    $(function() {
        $(".datepicker").datepicker({
            dateFormat: 'dd-mm-yy',
            beforeShowDay: my_check
        });
    });

    function my_check(in_date) {
        if (in_date.getDay() == 0) {
            return [false, "notav", 'Not Available'];
        } else {
            return [true, "av", "available"];
        }
    }
});

...and about a dozen other iterations... I obviously need to brush up on my coding in a big way (I know there are... so many problems there), but if anyone has any suggestions as to how I can fix this, I'd greatly appreciate it!
Posted
Updated 23-May-23 22:37pm
v2

1 solution

As already answered here[^], you need to use the beforeShowDay property:
beforeShowDay | Datepicker Widget | jQuery UI API Documentation[^]

You were close with your final code block - you just need to add the callback to the initial datepicker call, and get rid of the entire .ready block:
JavaScript
jQuery('#date-needed').datepicker({
    minDate: 0,
    maxDate: '+14d',
    beforeShowDay: function(date) {
        return date.getDay() === 0 ? [false, "notav", "Not Available"] : [true, "av", "Available"];
    }
});
 
Share this answer
 
Comments
Josh Mitchell 25-May-23 12:18pm    
Oh. Oh wow... nice of you to say that I was close, but I still think I was way off the mark. Your solution works perfectly - Mr. Deeming, you're my hero. Thank you so much!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900