How to set a default date in Bootstrap Datepicker?
I want to set a date : 24/12/2006
When I open datepicker it should show me this date select on calender.
This works for me after a long hours of search , This solution is for Bootstrap datetimepicker version 4. when you have to set the default date in input field.
HTML
"input type='text' class="form-control" id='datetimepicker5' placeholder="Select to date" value='<?php echo $myDate?>'// $myDate is having value '2016-02-23' "
Note - If You are coding in php then you can set the value of the input datefield using php, but datetimepicker sets it back to current date when you apply datetimepicker() function to it. So in order to prevent it, use the given JS code
JAVASCRIPT
<script>
$('#datetimepicker5').datetimepicker({
useCurrent: false, //this is important as the functions sets the default date value to the current value
format: 'YYYY-MM-DD'
});
</script>
A way of solution :
<div class="form-group">
<input type="text" class="form-control datepicker" name="arrivalDate" id="datepicker_arrival">
</div>
So in your js add this :
$('.datepicker').datepicker({
language: 'fr',
todayHighlight: true,
orientation: "bottom auto",
startDate: "today",
autoclose: true,
});
$('#datepicker_arrival').datepicker('setDate', new Date('2020/04/04'));
I didn't want to fire the changeDate event when I set to default date so I used
$('#calendarDatePicker').data("date", new Date());
Then instantiate the datepicker
$('#calendarDatePicker').datepicker();
Above accepted solution is staled, To help others who has the newest files, if you have Version 4 or newer, and you want to call a method, here is an example for bootstrap datetimepicker method :
$('#eventDate').data("DateTimePicker").date(moment(date).format('YYYY-MM-DD'));
bootstrap.datetimepicker format Option sample :
$('#eventDate').datetimepicker({ format: 'YYYY-MM-DD' });
for more see here (but without valid sample :) ): http://eonasdan.github.io/bootstrap-datetimepicker/Options/
You can do:
$('#dob').datepicker('setDate', new Date(2006, 11, 24));
$('#dob').datepicker('update');
$('#dob').val('');
Fiddle Demo
I wanted to set a date at the startup, and the mentionned methods didn't work. The date was shown, but when I clicked on the object the date was not selected.
So what I did is retrieving the Datepicker object of the library, and apply the _setDate method on it :
$('#dob').data("datepicker")._setDate(date);
But for this to work, the date object must be at midnight on UTC time, so before I did :
var date = new Date();
date.setUTCHours(0,0,0,0);
And then it worked.