Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set Min Date in Datepicker from another Datepicker?

I am currently adding validation to my date pickers and am having trouble setting the min date in the to date picker to be whatever was chosen in the from date picker. I.e if 12-3-15 is selected then the minimum date in the to date picker is 12-3-15. Here is the code I am using:

$("#from").datepicker({
            defaultDate: new Date(),
            minDate: new Date(),
            onSelect: function(dateStr) 
            {
            fromDate = new Date(dateStr);
            }
        });

$('#to').datepicker({
            defaultDate: new Date(),
            minDate: ("#from" + "1D"), (<- HERE)
            onSelect: function(dateStr) {
            toDate = new Date(dateStr);

                // Converts date objects to appropriate strings
                fromDate = ConvertDateToShortDateString(fromDate);
                toDate = ConvertDateToShortDateString(toDate);
        });

Does anybody know how I could achieve this?

like image 386
user2244961 Avatar asked Mar 16 '23 19:03

user2244961


1 Answers

Please check this working fiddle

JSFiddle

Use option to set the minDate to the second datepicker

$("#from").datepicker({
        defaultDate: new Date(),
        minDate: new Date(),
        onSelect: function(dateStr) 
        {         
            $("#to").val(dateStr);
            $("#to").datepicker("option",{ minDate: new Date(dateStr)})
        }
    });
like image 67
Madhu Avatar answered Mar 19 '23 07:03

Madhu