twitter-bootstrap bootstrap datetimepicker - 相对于现在设置 maxDate?

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

bootstrap datetimepicker - set maxDate relative to now?

javascripttwitter-bootstrapmomentjseonasdan-datetimepickermaxdate

提问by user3599803

I use this datetimepicker:
http://eonasdan.github.io/bootstrap-datetimepicker/Options/
Basically I want to set maxDate: moment(), i.e maxDate should be limited to now. The problem is by the time the datepicker is opened maxDate is not now() anymore. I want to set it maxDate to now everytime the datepicker is shown.

我使用这个 datetimepicker:
http: //eonasdan.github.io/bootstrap-datetimepicker/Options/
基本上我想设置maxDate: moment(),即 maxDate 应该限制到现在。问题是当 datepicker 打开时 maxDate 不再是 now() 了。每次显示日期选择器时,我都想将它的 maxDate 设置为 now。

I want to enforce it globally if possible, not to specify it on every datetimepicker()instance. Is it possible to somehow give a relative date to now using moment.js ?

如果可能,我想在全球范围内强制执行它,而不是在每个datetimepicker()实例上都指定它。是否可以使用 moment.js 以某种方式给出一个相对日期?

See this fiddle:
https://jsfiddle.net/0Ltv25o8/3281/
I set maxDate to now. You'll see after minute from the page load, you won't be able to choose the current minutes using the arrow buttons.

看到这个小提琴:
https:
//jsfiddle.net/0Ltv25o8/3281/ 我将 maxDate 设置为现在。您将在页面加载一分钟后看到,您将无法使用箭头按钮选择当前分钟。

回答by tata.leona

How about using custom version of datepicker plugin?

使用自定义版本的 datepicker 插件怎么样?

I have added new parameter maxDateNow(any ideas for a better name?) that allows selecting date/time up to present time.

我添加了新参数maxDateNow(有没有更好的名字的想法?),允许选择日期/时间直到当前时间。

$('#datetimepicker1').datetimepicker({maxDateNow: true, format:'DD/MM/YYYY HH:mm'});

See this fiddle

看到这个小提琴

回答by VincenzoC

You can listen to dp.showevent so you can change maxDateto the current time every time the picker shows. Here a code example:

您可以收听dp.show事件,以便maxDate每次选择器显示时都可以更改为当前时间。这是一个代码示例:

$('#datetimepicker1').datetimepicker({
  maxDate: moment(), 
  format:'DD/MM/YYYY HH:mm'
}).on('dp.show', function(){
  $('#datetimepicker1').data("DateTimePicker").maxDate(moment());
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

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

回答by Sheri Bgh

I used Liked This, It sets min Date to today's Date

我使用了 Liked This,它将最小日期设置为今天的日期

 $( '#Field_ID' ).datepicker({dateFormat: 'dd-MM-yy', minDate : new Date()});