bootstrap datetimepicker - set maxDate relative to now?

本小妞迷上赌 提交于 2019-12-10 20:39:15

问题


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.

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 ?

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.


回答1:


How about using custom version of datepicker plugin?

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

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

See this fiddle




回答2:


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

$('#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>



回答3:


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

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


来源:https://stackoverflow.com/questions/39022610/bootstrap-datetimepicker-set-maxdate-relative-to-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!