Javascript jquery datepicker 将日期设置为明天的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14591634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:19:00  来源:igfitidea点击:

jquery datepicker set date to tomorrow's date

javascriptjquerydatepicker

提问by Antonis Vergetakis

I have set the date for #arrival to todays date, but how can I set the #departure to tomorrow's date?

我已将#arrival 的日期设置为今天的日期,但如何将 #departure 设置为明天的日期?

 $(function() {

$( "#arrival" ).datepicker({

dateFormat: "dd/mm/yy", 
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: ":2016",
minDate: "dateToday",


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

});
$(function() {
    $("#arrival").datepicker("setDate", "0");
});


$( "#departure" ).datepicker({

dateFormat: "dd/mm/yy", 
changeMonth: true,
changeYear: true,
numberOfMonths: 2,
yearRange: ":2016",

});

$(function() {
    $("#departure").datepicker("setDate", "1");
});

});

I have customized the datepicker and it works fine.

我已经自定义了日期选择器,它工作正常。

回答by adeneo

In the change function of the first datepicker, create a date object, set the date one day forward, and set the date of the second datepicker to that date. You can use minDateto make sure any date earlier than the set date can not be picked.

在第一个日期选择器的更改函数中,创建一个日期对象,将日期向前设置一天,并将第二个日期选择器的日期设置为该日期。您可以使用minDate来确保不能选择早于设置日期的任何日期。

$(function () {
    $("#arrival").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        yearRange: ":2016",
        minDate: "dateToday",
        onClose: function (selectedDate) {
            var myDate = $(this).datepicker('getDate'); 
                myDate.setDate(myDate.getDate()+1); 
            $('#departure').datepicker('setDate', myDate);
        }
    });

    $("#departure").datepicker({
        dateFormat: "dd/mm/yy",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 2,
        yearRange: ":2016",
    });

    $("#arrival").datepicker("setDate", "0");
    $("#departure").datepicker("setDate", "1");
});

FIDDLE

小提琴