5

我从日期到现在都在这里。我想将我的 maxDate 设置为 3 个月取决于从日期选择。如果用户选择日期 01-01-2014,#to 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);
        }
    });
});

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

4

2 回答 2

13

小提琴演示

$(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);
        }
    });
});
于 2013-11-07T13:39:19.960 回答
3
$(function() {
    $( "#datepicker" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true
    });
});

这是文档

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

这是你的结果....干杯....

于 2013-11-07T13:16:35.787 回答