How to set the date in materialize datepicker

后端 未结 10 1226
长发绾君心
长发绾君心 2020-12-05 09:36

I am using materializecss.com Datepicker. When i try to set date with jquery, the date doesn\'t get set. Here is my Code :-

// Materialize Date Picker
    wi         


        
相关标签:
10条回答
  • 2020-12-05 10:33

    Alle answers with pickadate() don't work anymore in newer versions of Materialize. The method is now named datepicker(). Here's my code snippet:

    var element = document.querySelector('.datepicker');
    M.Datepicker.getInstance(element).setDate(new Date(2018,8,7), true);
    
    0 讨论(0)
  • 2020-12-05 10:34

    As per the docs materializecss.com Check the Date format options.

    // date picker
    $(document).ready(function(){
    
      $('.datepicker').datepicker( {"format":'dd-mm-yyyy'} ).datepicker("setDate", new Date());
    
    });
    

    You can remove and change the code to below if you do not need current date as highlighted in calendar,

    // date picker
    $(document).ready(function(){
    
      $('.datepicker').datepicker( {"format":'dd-mm-yyyy'});
    
    });
    
    0 讨论(0)
  • 2020-12-05 10:36

    If you are using angular, or something similar, in order to manage your web application and you are not able to instance the Datepicker object, according to the materialize documentation, using jQuery you can refer to these methods of the object:

    $('.datepicker').datepicker('setDate', new Date());
    

    In the first parameter, in this case setDate, you may specify the method name that you are about to use. In the second parameter, in this case new Date(), you will pass the value to the function (if any).

    In the example, that line will update the date of all datepickers with the class .datepicker to today.

    0 讨论(0)
  • 2020-12-05 10:40
    $('#elementID').pickadate('picker').set('select', '21/05/2017', { format: 'dd/mm/yyyy' }).trigger("change");
    

    This should suit your needs in one line. Trigger change you may need or maybe not, depends your situation, like me i need it for validation to trigger. Also include with format date just in case some people need it.

    0 讨论(0)
提交回复
热议问题