Set default date for datepicker in angular 2 material
if you are using reactive forms, below is working angular 10 code:
component.ts
this.youForm= this._formBuilder.group({
StartDate: [new Date(), Validators.required],
});
No change on component.html
<mat-form-field appearance="outline">
<input matInput [matDatepicker]="StartDate" placeholder="Start date *"
formControlName="StartDate">
<mat-label>Start Date *</mat-label>
<mat-datepicker-toggle matSuffix [for]="StartDate"></mat-datepicker-toggle>
<mat-datepicker #StartDate></mat-datepicker>
</mat-form-field>
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.