Avatar of Member_2_1242703
Member_2_1242703
 asked on

jQuery UI datepicker: startDate / endDate - Stopping selectable dates based on day of the week

Using some code now to prevent selecting an "End Date" before a "Start Sate"...

            var dateFormat = "mm/dd/yy";
            var from = $("#StartDate").datepicker({
                changeMonth: true,
                numberOfMonths: 1
            }).on("change", function () {
                to.datepicker("option", "minDate", getDate(this));
            });

            var to = $("#EndDate").datepicker({
                changeMonth: true,
                numberOfMonths: 1
            }).on("change", function () {
                from.datepicker("option", "maxDate", getDate(this));
            });

            function getDate(element) {
                var date;
                try {
                    date = $.datepicker.parseDate(dateFormat, element.value);
                } catch (error) {
                    date = null;
                }

                return date;
            }

Open in new window


I need to change this to also only allow a date to be selected up to the next sunday. i.e. looking at a current 2017 calendar, if I were to select today (Friday, March 10) as a "Start Date" then i would only be able to select dates through March 12, which is the next Sunday. (So only March 10, 11, and 12, would be available for "End Date"

Another example would be April 3 (Monday) is the start date.
April 4 (Tuesday)
April 5 (Wednesday)
April 6 (Thursday)
April 7 (Friday)
April 8 (Saturday)
April 9 (Sunday)
would be the only selectable options for "End Date"

How would I do this?
JavaScriptjQueryWeb DevelopmentBootstrapScripting Languages

Avatar of undefined
Last Comment
Brian Tao

8/22/2022 - Mon
Julian Hansen

Why not set To min date to today's date as well.
ASKER CERTIFIED SOLUTION
Brian Tao

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_1242703

ASKER
That's actually perfect Brian, thank you!
Brian Tao

You're welcome. One modification, line #10, #11 in my previous code should be:
      var nextSunday = new Date(fromDt.getTime());
      nextSunday.setDate(nextSunday.getDate() + 7 - fromDt.getDay());

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23