twitter-bootstrap 使用 Bootstrap Datetimepicker 的 defaultDate 和 minDate

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

defaultDate and minDate with Bootstrap Datetimepicker

jquerytwitter-bootstrapbootstrap-datetimepicker

提问by P_S

I want a picker to be set to a month in the future by default. This works fine:

我希望默认情况下将选择器设置为未来一个月。这工作正常:

<script type="text/javascript">
  $(function () {
    $('#enddatepicker').datetimepicker({
      defaultDate: moment().add(1, 'M').toDate(),
      locale: 'de',
      format: 'DD.MM.YYYY'
    });
  });
</script>

However, I also want the date to be not before tomorrow. So I try adding a minDate option:

但是,我也希望日期不早于明天。所以我尝试添加一个 minDate 选项:

<script type="text/javascript">
  $(function () {
    $('#enddatepicker').datetimepicker({
      minDate: moment().add(1, 'd').toDate(),
      defaultDate: moment().add(1, 'M').toDate(),
      locale: 'de',
      format: 'DD.MM.YYYY'
    });
  });
</script>

However, now the default date is set to minDate (that is, tomorrow). Can I set a default date independently from the minimum date?

但是,现在默认日期设置为 minDate(即明天)。我可以独立于最小日期设置默认日期吗?

回答by philreed

Looking at the code on GitHub(line 1674) if you set the option useCurrentto false, your default may not be overridden:

查看GitHub上的代码(第 1674 行),如果您将选项useCurrent设置为 false,则您的默认值可能不会被覆盖:

 <script type="text/javascript">
   $(function () {
     $('#enddatepicker').datetimepicker({
       useCurrent: false,
       minDate: moment().add(1, 'd').toDate(),
       defaultDate: moment().add(1, 'M').toDate(),
       locale: 'de',
       format: 'DD.MM.YYYY'
     });
   });
 </script>

回答by mccannf

I would raise an issue in githubfor this, as minDateshould not override defaultDate. In the mean-time, all you can do is set the value explicitly:

我会为此在 github 中提出一个问题,因为minDate不应该覆盖defaultDate. 同时,您所能做的就是明确设置该值:

$(function () {
    $('#enddatepicker').datetimepicker({
         minDate: moment().add(1, 'd').toDate(),
         locale: 'de',
         format: 'DD.MM.YYYY'
    });
     $('#enddatepicker').val(moment().add(1, 'M').format('DD.MM.YYYY'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
    <div class="demo col-xs-6">
       <input class="form-control" id="enddatepicker"/>
    </div>
  </div>
</div>

回答by user3040513

Try with Start Date property, after checked my jquery i found that has not minDate parameter but startDate parameter to handle minDate.it will work for you.

尝试使用 Start Date 属性,在检查我的 jquery 后,我发现没有 minDate 参数但 startDate 参数来处理 minDate.it 对你有用。

startDate :new Date(),

开始日期:新日期(),

skype @lokesh7676 to get more help

skype @lokesh7676 以获得更多帮助