如何在日期选择器中设置3个月的日期范围?

时间:2013-11-07 13:15:04

标签: jquery validation date datepicker

我从这里开始约会。我想将maxDate设置为3个月取决于从日期选择。如果用户从日期01-01-2014中选择,#maxDate应该是基于起始日期的下一个3个月,如(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);
        }
    });
});

给我一​​些解决问题的建议

2 个答案:

答案 0 :(得分:13)

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);
        }
    });
});

答案 1 :(得分:3)

$(function() {
    $( "#datepicker" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true
    });
});

以下是Documentation

$("#startDate").datepicker({
    minDate: 0,
    onSelect: function(selected) {
        var date = new Date(selected);
    date.setMonth(date.getMonth()+3);
        $("#endDate").datepicker("option","maxDate", date);
    }
});

这是你的男人......干杯......