Link to home
Start Free TrialLog in
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?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Why not set To min date to today's date as well.
ASKER CERTIFIED SOLUTION
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1242703
Member_2_1242703

ASKER

That's actually perfect Brian, thank you!
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