Javascript 如何在 bootstrap datetimepicker 中禁用从今天开始的过去日期?

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

How to disable past dates from today in bootstrap datetimepicker?

javascriptjquerytwitter-bootstrapdatetimepickereonasdan-datetimepicker

提问by Raviteja

Unable to disable the past dates in bootstrap datetimepicker

无法在 bootstrap datetimepicker 中禁用过去的日期

HTML

HTML

<br/>
<div class="row">
    <div class="col-md-12">
         <h6>Datetimepicker</h6>

        <div class="form-group">
            <div class="input-group date" id="datetimepicker">
                <input type="text" class="form-control" />  <span class="input-group-addon"><span class="glyphicon-calendar glyphicon"></span></span>
            </div>
        </div>

    </div>
</div>

JavaScript

JavaScript

$(function () {
    $('#datetimepicker').datetimepicker();
 });

I also used startDateatrribute.But no use.

我也用过startDate属性。但没用。

Fiddle here

在这里摆弄

回答by Shailendra Sharma

startDateoption use in bootstrap-datepickersame option not mentioned anywhere in bootstrap-datetimepicker/Options/doc it has minDateoption for that

startDate选项在bootstrap-datepicker 中使用的相同选项在bootstrap-datetimepicker/Options/doc中的任何地方都没有提到它有这个minDate选项

 $(function () {
     $('#datetimepicker').datetimepicker({  
         minDate:new Date()
      });
 });

FIddle

小提琴

回答by Atif Tariq

You can do it like this:

你可以这样做:

$(function () {
   var date = new Date();
   var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
   $('#datetimepicker').datetimepicker({ 
      minDate: today
   });
});

Another solution:

另一种解决方案:

$(function () { 
  var date = new Date();
  date.setDate(date.getDate()-1);
  $('#datetimepicker').datetimepicker({ 
   startDate: date
  });
});

回答by Manoj Salvi

I have used your fiddle and change it with following code which disables all the past dates, its working perfectly.

我已经使用了您的小提琴并使用以下代码对其进行了更改,该代码禁用了所有过去的日期,它运行良好。

Here is the Fiddleand Code -

这是小提琴和代码 -

$(function () {
   $('#datetimepicker').datetimepicker({
      minDate : moment()
   });
});