Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set 3 months date range in date picker?

I have from date and to date here. I want to set my maxDate to 3 months depends on the from date selection. If the user select from date 01-01-2014, #to maxDate should be next 3 months based on the from date, like (01-04-2014).

$(function () {
    $("#from").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',

        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#to").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        maxDate: selectedDate + "+3M", //I want to set condition like this

        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

give me some suggestion to solve my problem

like image 795
CJ Ramki Avatar asked Nov 07 '13 13:11

CJ Ramki


1 Answers

Fiddle DEMO

$(function () {
    $("#from").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        onClose: function (selectedDate, instance) {
            if (selectedDate != '') { //added this to fix the issue
                $("#to").datepicker("option", "minDate", selectedDate);
                var date = $.datepicker.parseDate(instance.settings.dateFormat, selectedDate, instance.settings);
                date.setMonth(date.getMonth() + 3);
                console.log(selectedDate, date);
                $("#to").datepicker("option", "minDate", selectedDate);
                $("#to").datepicker("option", "maxDate", date);
            }
        }
    });
    $("#to").datepicker({
        minDate: "dateToday",
        changeMonth: true,
        dateFormat: 'dd-mm-yy',
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});
like image 128
Tushar Gupta - curioustushar Avatar answered Sep 27 '22 22:09

Tushar Gupta - curioustushar