-->

Disabling Prior Dates But Leaving Field Blank?

2019-03-03 08:51发布

问题:

I am setting minDate: 'now'; in order to prevent picking dates prior to the current date. However, this populates the field with the current date. Not necessarily a big deal, however, if I am performing validation on the field (i.e., that it is populated), then the user can still submit even though not having picked a date.

Any way around this?

EDIT: Sorry, but it appears that using daysOfWeekDisabled shows the current date. Still not sure what to do.

回答1:

Your problem is similar to the one described here: issue #1289.

To solve it just set useCurrent to false. Here a working example:

$("#datetimepicker1").datetimepicker({
  minDate: moment(),  // min date = now
  useCurrent : false, // do not use current date by default
  daysOfWeekDisabled: [5] // disable some weekdays (e.g. fridays)
})
<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.42/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.15.2/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.42/js/bootstrap-datetimepicker.min.js"></script>

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



回答2:

What about this hack ? It doesn't allow selecting the previous day but it's still shown as enabled. Not sure if this will help.

("#mydp").datetimepicker({ minDate:(new Date()).setDate(new Date().getDate() - 1),
  useCurrent : false 
})