0

I have two jQuery UI date pickers on a form I am building. Most of the functionality is working as expected, except for one thing.

I have weekends disabled on both dates. However, when the defaultDate and minDate lands on a weekend, it doesn't select the next available week day but, rather, it still selects the weekend date.

Any help would be really appreciative :)

    var dateFormat = "dd/mm/yy",
    from = $("#from")
        .datepicker({
            dateFormat: dateFormat,
            beforeShowDay: $.datepicker.noWeekends,
            minDate: "+3d",
            defaultDate: "+3d",
            changeMonth: true
        })
        .on("change", function () {
            var date2 = from.datepicker('getDate')
            date2.setDate(date2.getDate() + 2)
            to.datepicker('setDate', date2)
            to.datepicker('option', 'minDate', date2)
        }),
    to = $("#to").datepicker({
        dateFormat: dateFormat,
        beforeShowDay: $.datepicker.noWeekends,
        defaultDate: "+1w",
        minDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
    })

UPDATE

I have solved the issue. Here is the answer for if anyone else is interested. I updated the day number (0-6) the selected date lands on and added so many days to it to avoid the weekend and stored this in a variable min. I then assigned the min variable to the defaultDate and minDate datepicker options :).

    var min = 3;

    switch (new Date().getDay()) {
        case 3:
        min = 5;
        break;
    case 4:
        min = 4;
        break;
    }

    var dateFormat = "dd/mm/yy",
    from = $("#from")
        .datepicker({
            dateFormat: dateFormat,
            beforeShowDay: $.datepicker.noWeekends,
            minDate: min,
            defaultDate: min,
            changeMonth: true
        })
        .on("change", function () {
            var date2 = from.datepicker('getDate')
            min = 2
            switch (date2.getDay()){
                case 4:
                    min = 4;
                    break;
                case 5:
                min = 4;
                break;
            }
            date2.setDate(date2.getDate() + min)
            to.datepicker('setDate', date2)
            to.datepicker('option', 'minDate', date2)
        }),
    to = $("#to").datepicker({
        dateFormat: dateFormat,
        beforeShowDay: $.datepicker.noWeekends,
        defaultDate: "+1w",
        minDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1
    })
  • check [this](https://stackoverflow.com/questions/501943/can-the-jquery-ui-datepicker-be-made-to-disable-saturdays-and-sundays-and-holid) – Margon Jun 20 '18 at 14:11
  • 1
    Possible duplicate of [Can the jQuery UI Datepicker be made to disable Saturdays and Sundays (and holidays)?](https://stackoverflow.com/questions/501943/can-the-jquery-ui-datepicker-be-made-to-disable-saturdays-and-sundays-and-holid) – Margon Jun 20 '18 at 14:12
  • Thanks @Margon. The problem I had was that even though I used the built in function beforeShowDay: $.datepicker.noWeekends to disable weekends, the minDate and defaultDate options were still selecting the weekend dates when I added like 3 days to selected day. I have seemed to of solved it now, and I have reflected that in my question above. If there is a simpler or more efficient way of doing this, I'd be happy to know :). – blj.davidson Jun 20 '18 at 16:15

0 Answers0