How to set the default date 10 years from today in bootstrap datepicker before clicking on the picker

Viewed 496

I am using Bootstrap-Datepicker. I would like to get today's date, minus 10 years from today's date, then show "(today's year-10)/1/1" as default display value, before the picker is displayed.

Right now, my code is as below. It shows "1990-1-1" as default.

    $(".datepicker").datepicker({
    // omitted
    }).datepicker("setDate", "1990-1-1");
  });

I can get today's date to show with the code below, but I can not get it to calculate minus 10 years. How do I do so?

}).datepicker("setDate", new Date());

FYI: This is the default date that needs to be shown:

Today's year - 10 / January / 1st
2 Answers

There are methods to set each data.

<script type="text/javascript">
var date = new Date();
date.setFullYear(date.getFullYear() - 10);
date.setMonth(0);
date.setDate(1);

  $(function() {
    $(".datepicker").datepicker({
      maxViewMode: "years",
      startView: "years",
    }).datepicker("setDate", date);
  });
</script>

First subtract 10 years from current date

 var date = new Date();
 date.setFullYear(date.getFullYear() - 10);

Then set that date in your datepicker ...

  }).datepicker("setDate", date);
Related