Jess
Jess

Reputation: 91

Bootstrap datepicker default date

I'm using Bootstrap datepicker and I'd like to make that when datepicker shows, default date would be set after 14 days from today. I've got this code but it doesn't work, default date is still set to today. Could anyone explain what am I missing? Thanks in advance.

JS:

  var plus14days = new Date();

  plus14days.setDate(plus14days.getDate() + 14 );
  $(".datepicker").datepicker("setValue", plus14days);

Upvotes: 9

Views: 30473

Answers (3)

Vishal Jain
Vishal Jain

Reputation: 501

var plus14days = new Date();

plus14days.setDate(plus14days.getDate() + 14 );
$('.datepicker').data("date", plus14days);
$(".datepicker").datepicker();

You can try to set the default date in datepicker data and then initialize the datepicker. This worked for me.

Upvotes: 0

Luca Trazzi
Luca Trazzi

Reputation: 1260

Just a consideration, api changed setValue to setDate (http://bootstrap-datepicker.readthedocs.org/en/release/methods.html#setdate)

Upvotes: 4

Praveen
Praveen

Reputation: 56501

The code you've used is right.

The date will be displayed in textbox, but not in datepicker calendar like this

enter image description here

To enable this feature you need to update it using the following code:

$(".datepicker").datepicker('update');

Now it looks like

enter image description here

Check this in JSFiddle

Upvotes: 13

Related Questions