Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict date range of a jquery datepicker by giving two dates?

Tags:

I am having two dates that is stored in db and am selecting it using $.ajax() and what i need is to show the datepicker values between the dates I selected from db.

Here is my code for it.But it is not working properly

function setDatePickerSettings(isFisc) {
        var fSDate, fEDate;
        $.ajax({
            type: "POST",
            url: '../Asset/Handlers/AjaxGetData.ashx?fisc=1',
            success: function(data) {
                alert(data);
                var res = data.split("--");//data will be 4/4/2010 12:00:00--5/4/2011 12:00:00 
                var sDate = res[0].split(getSeparator(res[0]));
                alert("Separator " + getSeparator(res[1]) + " Starts " + sDate);
                var eDate = res[1].split(getSeparator(res[1]));
                alert("End " + eDate);
                alert("sub " + sDate[0]);
                fSDate = new Date(sDate[2].substring(0, 4), sDate[0], sDate[1]);
                alert("Starts " + fSDate.substring(0, 4));
                fEDate = new Date(eDate[2].substring(0, 4), eDate[0], eDate[1]);
                alert("eND " + fEDate.toString());

            }

        });
          var dtSettings = {
        changeMonth: true,
        changeYear: true,
        showOn: 'both',
        buttonImage: clientURL + 'images/calendar.png',
        buttonImageOnly: true,
        showStatus: true,
        showOtherMonths: false,
        dateFormat: 'dd/mm/yy',
        minDate:fSDate, //assigning startdate
        maxDate:fEDate //assigning enddate
    };

    return dtSettings;
}

Pls provide some solution. I need the datetime picker which requires values between that range. Thanks in advance

like image 597
kbvishnu Avatar asked Dec 27 '10 10:12

kbvishnu


People also ask

How do I restrict dates on date picker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).

How do I set a datepicker range?

Steps to add Datepicker – $(document). ready(function(){ $("#setMin,#setMax"). datepicker({ dateFormat: "yy-mm-dd" }); $('#datepicker'). datepicker({ dateFormat: "yy-mm-dd", maxDate:'+1m +10d', minDate: -10 }); });


2 Answers

Your syntax is wrong for the minDate/maxDate. You can read the documentation on the jQuery UI website where the demo is. I suggest you take a look at it to tailor it to your specific case. It looks something like this:

$( ".selector" ).datepicker({ minDate: new Date(2007, 1 - 1, 1) });

or

the following will make it so that you can't pick anything previous to today.

$( ".selector" ).datepicker({ minDate: 0 });

and this will make it so you can't pick anything before tomorrow

$( ".selector" ).datepicker({ maxDate: 1 });

Edit: Here is how you insert your own date, but i'm having an issue getting the dateFormat to work correctly, as you can see I have the dateFormat specified, but the format I actually put in is ignoring the dateformat.

$("#txtDateStart").datepicker({dateFormat:'mm/dd/yy', minDate: new Date(2010,11,12) });
like image 152
The Muffin Man Avatar answered Oct 15 '22 02:10

The Muffin Man


I know this is an ancient post but the bug that @TheMuffinMan raised about not being able to get the date format working with the date restriction options is real and appears to only surface when the options are in-line as in his example.

If you have to use this format, and if anyone is still interested, the way around it is to put the date formatting options as the last option in the set. For example the code below works flawlessly for me.

var minDate = -20;
var maxDate = "+1M +10D" 

$('body').on('focus',".datepicker", function(){
    $(this).datepicker({ minDate: minDate, maxDate: maxDate },{dateFormat: "dd/mm/yy"});
});

I hope it helps someone else.

like image 37
Alexander Avatar answered Oct 15 '22 02:10

Alexander