Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put a default date in bootstrap datetimepicker

I want my datetimepicker to have a default value which is the current date. How can I achieve this? I already read the documentation but can't find anything to achieve that. Can someone help me? I want the format of the default value is MM dd, yyyy.

enter image description here

Here's my code.

<div class="form-group">
  <label for="dtp_input2">Date</label>
  <div class="input-group date form_date col-md-3" data-date="" data-date-format="MM dd, yyyy">
    <input id="dtp_input2" name="date" class="form-control" size="10" type="text" value="" readonly>
    <span class="input-group-addon">
      <span class="glyphicon glyphicon-remove"></span>
    </span>
    <span class="input-group-addon">
      <span class="glyphicon glyphicon-calendar"></span>
    </span>
  </div>
</div>
$('.form_date').datetimepicker({
  weekStart: 1,
  todayBtn: 1,
  autoclose: 1,
  todayHighlight: 1,
  startView: 2,
  minView: 2,
  forceParse: 0,
});
like image 519
nethken Avatar asked Jan 27 '17 15:01

nethken


2 Answers

Just set the value after setting up the datetimepicker:

$('.form_date').datetimepicker({
  weekStart: 1,
  todayBtn: 1,
  autoclose: 1,
  todayHighlight: 1,
  startView: 2,
  minView: 2,
  forceParse: 0,
});
$('#dtp_input2').val(Date());
like image 93
Matt Spinks Avatar answered Sep 27 '22 19:09

Matt Spinks


In my case : Insert this defaultDate: new Date() code into your javascript datetimepicker.

<script type="text/javascript">
$(function () {
    $('#datetimepicker10').datetimepicker({
        defaultDate: new Date(),
        viewMode: 'years',
        format: 'MM/DD/YYYY'
    });
});

like image 29
gustav Avatar answered Sep 27 '22 19:09

gustav