Search code examples
javascriptjquerymaterialize

Setting Date Picker to select a Birthdate in the past MaterializeCss


I am trying to use MaterializeCss date-picker form field to set a birthday.

  $('.datepicker').pickadate({
    selectMonths: true, // Creates a dropdown to control month
    selectYears: 15 // Creates a dropdown of 15 years to control year
  });

is what i am currently using, however this gives a total of 15 years in total 7 years before todays year and 7 years after. how can I only allow for present year - 80 years for example..

if I do something like:

  $('.datepicker').pickadate({
    selectMonths: true, // Creates a dropdown to control month
    selectYears: -15 // Creates a dropdown of 15 years to control year
  });

the year menu totally disappears.

How can I achieve this?


Solution

  • You can use the max property to limit the date range to the current year, like so.

    var date = new Date();
    var today = '12/31/' + date.getFullYear();
    
    $('.datepicker').pickadate({
      format: 'mm/dd/yyyy',
      selectMonths: true,
      selectYears: 15,
      max: today,
    });
    

    Also see http://amsul.ca/pickadate.js/date/ for more options.