Search code examples
angularangular-materialangular-material2

Set default date for datepicker in angular 2 material


I'm trying to create datepicker using material.angular.io, but I need to set default date for field. In previose version angular material it was easy. Somebody knows how I can do it now?

my code:

<input mdInput
       name="start_time"
       #start_time="ngModel"
       [mdDatepicker]="startDate"
       [min]="minDate"
       date="true"
       [(ngModel)]="planModel.start_time"
       placeholder="Choose a date">

    <md-datepicker-toggle mdSuffix [for]="startDate"></md-datepicker-toggle>
    <md-datepicker [startView]="dateStart" #startDate></md-datepicker>

Solution

  • You need to initialize your date model. In your ts code, do the following in constructor or in the method where you get planModel:

    planModel.start_time = new Date(); // Current Date
    

    Link to working demo.