Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...