I'm using a date picker from angular material. I want to set a default value but it is not showing the value.
<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
<input matInput [picker]="picker" placeholder="Date"
autocomplete="off"
name="date"
formControlName="date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker [startAt]="startDatePicker" #picker></mat-datepicker>
</mat-form-field>
this is my .js code with the value that i want to set as default
var date = this.datepipe.transform((new Date().getTime()) - 3888000000, 'dd/MM/yyyy');
this.form = this.formBuilder.group({
dataInicial: [data_inicial],
...
mat-datepicker exampleAdd a template reference variable mat-datepicker element. Connect mat-datepicker to input element via [matDatepicker] property. Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element.
This works for me!
HTML-
<mat-form-field>
<input matInput [matDatepicker]="picker1" placeholder="From Date" [formControl]="date1">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
TS-
date1 = new FormControl(new Date())
You need to provide a Date object to the startAt change as below:
In .ts:
date = new Date((new Date().getTime() - 3888000000));
In html:
<mat-datepicker [startAt]="date" #picker></mat-datepicker>
A working demo here: https://stackblitz.com/edit/angular-n9yojx
You can use the formControl defined and in your input.
here is html
<mat-form-field class="mr-sm-24" fxFlex (click)="open()" >
<input [matDatepicker]="picker" matInput placeholder="Date" autocomplete="off" name="date" formControlName="date" [(ngModel)]="date.value">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
here is TS declare you formControl
date: FormControl;
this.date = new FormControl(new Date(<you can provide you date input field if you getting date from other sources>))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With