user5297740
user5297740

Reputation: 147

Set tomorrow value in input bootstrap datetimepicker

I have an input type associated to a bootstrap datetimepicker.

Code :


<script type="text/javascript">
   var date = new Date();
   date.setDate(date.getDate());
   $(document).ready(function () 
   {
      $('#datetimepicker').datepicker(
      {
         autoclose: true,
         daysOfWeekDisabled: '06',
         format: 'dd/mm/yyyy',
         showWeeks: false,
         startDate: '+1d',
         maxViewMode: 'days',
         language: 'it'
      });
  });
</script>
<div class="col-lg-4 col-md-6 col-sm-6 col-xs-12" style="margin-top:2%;">
   <input type="text" value="" class="formLocator" id="datetimepicker">
</div>

I want the default value in the input box to be set to tomorrow but also only from among the days allowed.

How can I achieve this?

Upvotes: 0

Views: 859

Answers (1)

stark
stark

Reputation: 2256

Just add the setDate key with a value of the date object that you want the default date to be.

$('#datepicker').datepicker({
        setDate: new Date()
});

Or in such a way :

$('#datetimepicker').datepicker('setDate', startDate);

Check this fiddle here : fiddle

$(document).ready(function() {
  var today = new Date();
  var today_day = today.getDay();
  var jump = 0;
  if (today_day == 5) {
    jump = 3;
  } else if (today_day == 6) {
    jump = 2;
  } else {
    jump = 1;
  }
  var tomorrow = new Date(new Date().getTime() + jump * 24 * 60 * 60 * 1000);
  var month = tomorrow.getMonth();
  var year = tomorrow.getFullYear();
  var date = tomorrow.getDate();
  var startDate = new Date(year, month, date);
  $('#datetimepicker').datepicker({
    autoclose: true,
    daysOfWeekDisabled: '06',
    format: 'dd/mm/yyyy',
    showWeeks: false,
    startDate: '+1d',
    maxViewMode: 'days',
    language: 'it'
  });
  $('#datetimepicker').datepicker('setDate', startDate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.1/js/bootstrap-datepicker.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-lg-4 col-md-6 col-sm-6 col-xs-12" style="margin-top:2%;">
  <input type="text" class="formLocator" id="datetimepicker">
</div>

Upvotes: 2

Related Questions