I want my angular material datepicker to only show month/year. NO days.
Here is my datepicker:
<mat-form-field>
<input matInput [matDatepicker]="picker" placeholder="Choose a date">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
I saw previous threads stating setting the mode to:
md-mode="month"
The above does not work anymore. What is the solution now?
To disable only datepicker toggle we can add dispabled property to the mat-datepicker-toggle element. In this case user can only enter date by manually typing.
MAT Calendar-Maharashtra Administrative Tribunal, Mumbai.
A datepicker is composed of a text input and a calendar pop-up, connected via the matDatepicker property on the text input. There is also an optional datepicker toggle button that gives the user an easy way to open the datepicker pop-up.
Here is a code that worked for me. I see you are trying something similar. Hope it helps.
The .html
file:
<mat-form-field>
<input matInput [matDatepicker]="dp2" [min]="sixMonthsAgo" [max]="today" placeholder="Select a Date" (click)="openDatePicker(dp2)">
<mat-datepicker-toggle matSuffix [for]="dp2"></mat-datepicker-toggle>
<mat-datepicker #dp2 startView="multi-year" (monthSelected)="closeDatePicker($event, dp2)"></mat-datepicker>
</mat-form-field>
The snippet from .ts
file:
this.today = new Date();
this.sixMonthsAgo = new Date();
this.sixMonthsAgo.setMonth(this.today.getMonth() - 6);
openDatePicker(dp) {
dp.open();
}
closeDatePicker(eventData: any, dp?:any) {
// get month and year from eventData and close datepicker, thus not allowing user to select date
dp.close();
}
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