为引导日期时间中的字段设置默认时间

时间:2016-08-08 01:46:49

标签: jquery twitter-bootstrap bootstrap-datetimepicker

$(function() {
  $('.date').datetimepicker({
    format: 'YYYY-MM-DD HH:mm:ss'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.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://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>


<div class="container panel">
<div class="container panel-heading"><h1>
</h1>Choose time</div>
</div>
  <div class="container panel-body">
    <div class="row">
      <div class='col-sm-6'>
        <div class="form-group">
          <label>Start time</label>
          <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>
        </div>
        <div class="form-group">
          <label>End time</label>
          <div class='input-group date' id='datetimepicker2'>

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

我想为下面的两个datetimepickers设置默认时间。除非另有说明(用户),否则第一个(开始时间)应为00:00:01。除非另有说明(用户),否则第二个(结束时间)应为23:59:59。

datetimepicker似乎提供默认日期选项,但没有默认时间选项。

1 个答案:

答案 0 :(得分:2)

您可以将Date对象传递给defaultDate参数。

var now = new Date();
var def1 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 1);
var def2 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59);

$(function() {
  $('.date:eq(0)').datetimepicker({
    defaultDate: def1,
    format: 'YYYY-MM-DD HH:mm:ss'
  });
  $('.date:eq(1)').datetimepicker({
    defaultDate: def2,
    format: 'YYYY-MM-DD HH:mm:ss'
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.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://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>


<div class="container panel">
  <div class="container panel-heading">
    <h1>
</h1>Choose time</div>
</div>
<div class="container panel-body">
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <label>Start time</label>
        <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>
      </div>
      <div class="form-group">
        <label>End time</label>
        <div class='input-group date' id='datetimepicker2'>

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