Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
392 views
in Technique[技术] by (71.8m points)

javascript - jquery datepicker set date to tomorrow's date

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

 $(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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 minDate to make sure any date earlier than the set date can not be picked.

$(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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...