NoviceDeveloper
NoviceDeveloper

Reputation: 1290

JQuery datepicker range and start date

I have an input box with id myDate where I want users to enter dates NOT before 11/01/2014 and not after 12/30/2014. I also want to add 2 weeks advance notice from current date.

$("#myDate").datepicker({
    setDate: '11/01/2014',
    minDate: 14,
    maxDate: '12/30/2014',
    beforeShowDay: $.datepicker.noWeekends
});

but as you can see it does not work. the set date is not working it only takes from current date and applies 14 days advance.

Upvotes: 0

Views: 199

Answers (2)

NoviceDeveloper
NoviceDeveloper

Reputation: 1290

Well after not getting anyone to answer this question. I came up with my own.

Hope this helps someone like myself...

$(function () {
    var now = new Date();
    if (now >= Date.parse('11/01/2014')) {
        $('#dateFrom').datepicker({
            minDate: 14,
            maxDate: '12/30/2014',
            beforeShowDay: $.datepicker.noWeekends
        });
    } else {

        var dtDate1 = '11/01/2014'
        var dtDate2 = dtDate1.replace(/-/g, '/')
        var nDifference = Math.abs(new Date() - new Date(dtDate2));
        var one_day = 1000 * 60 * 60 * 24;
        var days = Math.round(nDifference / one_day);

        if (Math.round(nDifference / one_day >= 14)) {
            $('#dateFrom').datepicker({
                minDate: '11/01/2014',
                maxDate: '12/30/2014',
                beforeShowDay: $.datepicker.noWeekends
            });
        }
        else
            if (Math.round(nDifference / one_day < 14)) {
            $('#dateFrom').datepicker({
                minDate: days,
                maxDate: '12/30/2014',
                beforeShowDay: $.datepicker.noWeekends
            });
        }
    }
})

Upvotes: 1

jatwater
jatwater

Reputation: 1

Unfortunately, I think you're going to have to write your own custom function to account for the advanced notice piece. I think that's beyond the jQuery Datepicker capability. Also if you want your users to NOT pick a date before 11-1 why not just have 11-1 as your minDate?

Upvotes: 0

Related Questions