jQuery Bootstrap 中的开始日期和结束日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28381303/
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-26 16:28:01  来源:igfitidea点击:

Start Date and End Date in Bootstrap

jquerytwitter-bootstrapvalidationdatedatepicker

提问by Aravinthan K

I am using Bootstrap DatePicker. I want to Validate From Dateand 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?

我正在使用 Bootstrap DatePicker。我想验证From DateTo Date。开始日期正确选择今天的日期。我有一个问题“迄今为止没有选择开始日期(即从日期值)”。如何解决?

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

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

回答by Artur Filipiak

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

在您设置时$('#toDate').datepicker(), 的值为$('#fromDate')空。

You should set default startDateand endDatevariables at the begining and add changeDateevent listener to your datepickers.

您应该在开始时设置默认值startDateendDate变量,并将changeDate事件侦听器添加到您的日期选择器。

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()));
});