Datepicker结束日期选择器年份范围从开始日期年份中选择

时间:2014-08-04 09:37:05

标签: jquery datepicker

$('#stpartdates').datepicker({
    changeMonth: true,
    changeYear: true,
    onSelect: function(date) {
        var date2 = $('#stpartdates').datepicker('getDate');
        date2.setDate(date2.getDate() + 365);
        $('#etpartdates').datepicker('setDate', date2);
        $('#etpartdates').datepicker('option', 'minDate', date2);
    }
});
$('#etpartdates').datepicker({
    changeMonth: true,
    changeYear: true,
    minDate: $('#stpartdates').val(),
    onSelect: function() {
        //  $("#ui-datepicker-div").hide();
    }
});

stpartdates是一个开始日期,etpartdates是结束日期, 如果用户选择04-08-2014我想显示结束日期的年份范围仅为2015

我的意思是

start date year + 1 = end date yearrange

2 个答案:

答案 0 :(得分:0)

试试这个

var date = $(this).datepicker('getDate'),
    year = date.getFullYear() + 1;
$("#etpartdates").datepicker("option", "minDate", "01/01/" + year);

而不是

$('#etpartdates').datepicker('setDate', date2);

FIDDLE DEMO

答案 1 :(得分:0)

尝试以下代码

    $("#stpartdates,#etpartdates").datepicker({
    changeMonth: true,
    changeYear: true,
    onSelect: function (dateText, obj) {
        var MinDate = new Date(dateText);
        var MaxDate = new Date(dateText);
        var newDateRangeMax = new Date(MinDate.getFullYear()+1,MinDate.getMonth(),MinDate.getDate());
        var newDateRangeMin = new Date(MaxDate.getFullYear()-1, MaxDate.getMonth(),MaxDate.getDate());
        if ($(this).attr("id") == "stpartdates") {
            $("#etpartdates").datepicker("option", "minDate", newDateRangeMin );
        }
        else{

            $("#stpartdates").datepicker("option", "maxDate", newDateRangeMax)

        }
    });