Search code examples
javascriptjquerybootstrap-datetimepicker

How to have datepicker set default year?


I am using bootstrap datepicker and I only show the years. When I load the page, I would like to set a default year, tried the following but the current year (2018) is hightlited instead.

$('#sandbox-container div').datepicker({
  format: "yyyy",
  viewMode: "years", 
  minViewMode: "years",
  updateViewDate: false,
  changeYear: true,
  defaultDate: '2014'
});

Thing is this is an Edit page, so the year is coming from the db, therefore I need to be able to set a default year as per the value I am getting from the db.


Solution

  • I had to change updateViewDate: false into updateViewDate: true, so:

    $('#sandbox-container div').datepicker({
      format: "yyyy",
      viewMode: "years", 
      minViewMode: "years",
      updateViewDate: true,
      changeYear: true,
      defaultViewDate: {year: '2014'}
    });