Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the date in materialize datepicker

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
    window.picker = $('.datepicker').pickadate({
        selectMonths: true, // Creates a dropdown to control month
        selectYears: 100, // Creates a dropdown of 15 years to control year
        format: 'dd/mm/yyyy'    
    });

<input type="text" id="Date" class="datepicker" />

On Click event of a Button , I am setting the date :-

$("#Date").val('23/01/2015');

When i open the datepicker it shows me today's date.

How to set the date in materialize datepicker?

like image 460
Anup Avatar asked May 19 '15 11:05

Anup


4 Answers

Materialize datepicker is a modified pickadate.js picker.

Accodging to their API docs, this is how to set the picker:

  1. Get the picker:

var $input = $('.datepicker').pickadate()

// Use the picker object directly.
var picker = $input.pickadate('picker')
  1. Set the date:

// Using arrays formatted as [YEAR, MONTH, DATE].
picker.set('select', [2015, 3, 20])

// Using JavaScript Date objects.
picker.set('select', new Date(2015, 3, 30))

// Using positive integers as UNIX timestamps.
picker.set('select', 1429970887654)

// Using a string along with the parsing format (defaults to `format` option).
picker.set('select', '2016-04-20', { format: 'yyyy-mm-dd' })
like image 72
art-solopov Avatar answered Nov 16 '22 07:11

art-solopov


just add the format you want your date looks like in the attributes of your element.

$('.datepicker').pickadate({
    selectMonths: true, // Creates a dropdown to control month
    selectYears: 15, // Creates a dropdown of 15 years to control year
    format: 'dd-mm-yyyy' });
like image 37
Gabriel Avatar answered Nov 16 '22 06:11

Gabriel


You can use methods of datepicker which are present in V1.0.0-rc.2.

document.addEventListener('DOMContentLoaded', function() {
  var options = {
    defaultDate: new Date(2018, 1, 3),
    setDefaultDate: true
  };
  var elems = document.querySelector('.datepicker');
  var instance = M.Datepicker.init(elems, options);
  // instance.open();
  instance.setDate(new Date(2018, 2, 8));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />

<input type="text" class="datepicker">

defaultDate will set a default Date that will be shown in input-field of datepicker without even opening the picker.

enter image description here

instance.setDate() will select the date in datepicker when you'll open it.

enter image description here

Note- new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);

The argument monthIndex is 0-based. This means that January = 0 and December = 11

Picker - Materialize

MDN - Date

like image 11
Germa Vinsmoke Avatar answered Nov 16 '22 06:11

Germa Vinsmoke


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);
like image 6
hendrikbeck Avatar answered Nov 16 '22 06:11

hendrikbeck