Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material Datepicker - Month & Year ONLY

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?

like image 613
user10181542 Avatar asked Nov 03 '19 02:11

user10181542


People also ask

How do I disable matDatepicker?

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.

What is Mat calendar?

MAT Calendar-Maharashtra Administrative Tribunal, Mumbai.

What is Mat-datepicker-toggle?

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.


1 Answers

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();    
  }
like image 69
M M Avatar answered Sep 18 '22 12:09

M M