Search code examples
jquerydatepickermaterialize

Default Date a year ago from today using materialize


Is a possible using default date a year ago using the latest materialize js version?

because the documentation, just showing

minDate - Date(type) - null (Default) - The earliest date that can be selected. (description)

https://materializecss.com/pickers.html

I try use like this

$('.datepicker').datepicker({
  minDate : new Date()- 360
});

It didn't work fine. By the way I still confused that I should use minDate or defaultDate. Any idea? here is the demo: https://jsfiddle.net/dedi_wibisono17/72a5ugyv/2/

thank you


Solution

  • You can minus 1 current year and then pass date to minDate :

    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var date = new Date(year - 1, month, day);
    
    $('.datepicker').datepicker({
      autoClose : true,
      format : 'dd mmm yyyy',
      minDate:  date
    });
    

    jsfiddle