Start Date and End Date in Bootstrap

2020-07-10 08:12发布

I am using Bootstrap DatePicker. I want to Validate From Date and To Date . Start Date correctly Pick todays date.I have a Problem " To Date does not Pick the start date(ie. from date value)" .How to solve it?

  $(document).ready(function() {
    $('#fromDate').datepicker({
        startDate: new Date(),
    });

    $('#toDate').datepicker({
        startDate: $('#fromDate').val(),
    });
 });

1条回答
Viruses.
2楼-- · 2020-07-10 08:39

At the time you set $('#toDate').datepicker() , the value of $('#fromDate') is empty.

You should set default startDate and endDate variables at the begining and add changeDate event listener to your datepickers.

Eg.:

// set default dates
var start = new Date();
// set end date to max one year period:
var end = new Date(new Date().setYear(start.getFullYear()+1));

$('#fromDate').datepicker({
    startDate : start,
    endDate   : end
// update "toDate" defaults whenever "fromDate" changes
}).on('changeDate', function(){
    // set the "toDate" start to not be later than "fromDate" ends:
    $('#toDate').datepicker('setStartDate', new Date($(this).val()));
}); 

$('#toDate').datepicker({
    startDate : start,
    endDate   : end
// update "fromDate" defaults whenever "toDate" changes
}).on('changeDate', function(){
    // set the "fromDate" end to not be later than "toDate" starts:
    $('#fromDate').datepicker('setEndDate', new Date($(this).val()));
});
查看更多
登录 后发表回答