4
votes

I was looking for a way to make the angular material datepicker 2 change to only year and month mode but it does not have that option in the documentation

I can set the startView but when month or year is selected, change to Day mode

<md-form-field>
     <input mdInput [mdDatepicker]="picker" placeholder="Choose a date">
     <md-datepicker-toggle mdSuffix [for]="picker"></md-datepicker-toggle>
     <md-datepicker #picker startView="year" [startAt]="startDate"></md-datepicker>
</md-form-field>

I want this: https://plnkr.co/edit/eV2Kmt?p=preview

But that plunker is in angular material 1

Any help? :)

5

5 Answers

3
votes

For this time this is an open bug. See this link

0
votes

Looks like there's soon gonna be year support soon, once this PR is merged! 🎉

(However, it appears that it probably will appear in release 5.1 since it was placed in milestone 5.1)

0
votes

In HTML

<mat-datepicker #searchDate startView="year" (monthSelected)="monthSelectedHandler($event)"></mat-datepicker>

In your controller

searchDateValue: FormControl;

ngOnInit() {
    this.searchDateValue = new FormControl(this.searchDate);
}

monthSelectedHandler(event) {
    const date = new Date(`${event._i.month + 1}/${event._i.date}/${event._i.year}`);
    this.searchDateValue.setValue(date);
    this.searchDatePicker.close();
}
0
votes

Checkout this repo, it has also a stand alone month picker: https://github.com/vlio20/angular-datepicker

0
votes

it will helpful material datepicker with month and year format.check link for MM-YYYY