1

Is it possible to specify the date during initialization using the Eternicode Bootstrap Datepicker? By default, when initializing the picker, the current date will be selected. I want a different date (in the future) to be selected. Is this possible and how so?

Here is my initialization code:

datePicker = detePickerElem.datepicker({
    format: 'mm/dd/yyyy',
    maxViewMode: 'months',
    startDate: new Date(),
    todayBtn: true,
    todayHighlight: true,
    weekStart: 1
});

datePicker.datepicker('setDate', new Date(futureDateObject));

I've tried using the setDate method to set the date after initialization, but this doesn't do anything. Note that for various reasons I'm using an older release (v1.3) of the project and am not able to migrate to a newer release, however I can fork the repo and modify the source if needed.

Daniel Bonnell
  • 4,817
  • 9
  • 48
  • 88
  • Have you tried assigning `startDate` something other than the current Date? – Jasen Aug 05 '16 at 17:47
  • startDate would work; but also disable the ability to choose any date before that date... – Robert Aug 05 '16 at 17:47
  • You give the input element the default value `datePickerElem.value = theDate`. – Jasen Aug 05 '16 at 17:53
  • And your `setDate` method should work. Do you have any errors on the debug console? – Jasen Aug 05 '16 at 17:55
  • I tried `$('.datepicker').siblings('input').val(new Date(futureDateObject));`. That set the input value but didn't solve my problem. The datepicker still shows today selected. – Daniel Bonnell Aug 05 '16 at 18:32
  • I also tried changing `startDate` but that just sets `futureDateObject` as the minimum selectable date, which is not what I want. – Daniel Bonnell Aug 05 '16 at 18:34

2 Answers2

3

Since you mentioned you are using eternicode Bootstrap datepicker, if you look at the options on https://github.com/eternicode/bootstrap-datepicker/blob/ca11c450/README.md#options there is a way to set a custom startdate as below:

$('#datepicker').datepicker('setStartDate', '2015-01-01');

Another thread that might be of help here its shown how to increment by 1 day: Bootstrap DatePicker, how to set the start date for tomorrow?

Community
  • 1
  • 1
  • This sets the minimum selectable start date, which is not what I want. I want to change the *currently selected date*. – Daniel Bonnell Aug 05 '16 at 20:53
  • I use this for jQuery datepicker to set change the selected date: `$('#dateselector').datepicker("setDate", new Date(2016,8,21) )` let me know if this helps – Pavithra Olety Aug 12 '16 at 23:30
  • That only changes the date, but does not update the calendar UI. Unfortunately because this was for a work project I had to use a different datepicker. – Daniel Bonnell Aug 14 '16 at 13:24
1

If you want date selection greater than today, you should set start date tomorrow. Also no need for todayBtn when it isn't selectable.

var tomorrow = new Date();
tomorrow.setDate(new Date().getDate()+1);

datePicker = detePickerElem.datepicker({
    format: 'mm/dd/yyyy',
    maxViewMode: 'months',
    startDate: tomorrow,
    todayBtn: false,
    todayHighlight: true,
    weekStart: 1
});

datePicker.datepicker('setDate', tomorrow);
HasanG
  • 12,734
  • 29
  • 100
  • 154