'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?
Solution 1:[1]
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();
}
Solution 2:[2]
The requirement is to show "MMMM YYYY" format on the input element. "MMMM YYYY" format is "Month Year". Want to create custom date format and apply to the mat-datepicker.
in the HTML
<mat-label>For the Month</mat-label>
<input matInput [matDatepicker]="picker1" name="CalMonth" formControlName="CalMonth">
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1 disabled="false"></mat-datepicker>
in the .ts typescript
want to import material-moment-adapter ,material/core and moment
To install the material-moment-adapter
npm i @angular/material-moment-adapter
npm i angular-moment
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import * as moment from 'moment';
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'MMMM YYYY', // this is the format showing on the input element
monthYearLabel: 'MMMM YYYY', // this is showing on the calendar
},
};
after that add to the component
@Component({
selector: 'app-monthselector',
templateUrl: './monthselector.component.html',
styleUrls: ['./monthselector.component.scss'],
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
after that add the format to the relevant controller
this.UIForm = new FormGroup({
CalMonth: new FormControl(moment()),
});
follow above steps one by one and you can customize the date format to "Month Year" .
you can change anyother date format as well, only change this
dateInput: 'MMMM YYYY',
Thanks,
Indika Pradeep
Solution 3:[3]
This is surprisingly not straightforward.. I have found the "official" way here
https://stackblitz.com/angular/ekjvrbglvnb?file=src%2Fapp%2Fdatepicker-views-selection-example.ts
Solution 4:[4]
Code in .html
<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>
Code in .ts
import {MatDatepicker} from '@angular/material/datepicker';
@ViewChild('picker') datePickerElement = MatDatepicker;
ngOnInit() {
console.log(this.datePickerElement)
console.log(this.datePickerElement['opened'])
setTimeout(() => {
this.datePickerElement['_datepickerInput']._dateFormats = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'MMMM YYYY',
monthYearLabel: 'MMMM YYYY',
},
};
},1000)
}
This is working fine for me.
Date format will display like below screenshot

Solution 5:[5]
I have worked out a solution, fully relying on the example given in the official Angular Material documentation :
The example with code works fine but there is still a navigation to 'month' view in the calendar when user clicks on years in the header
Here is a turnaround : in the TS of the custom component where the
<mat-datepicker #dp startView="multi-year" (yearSelected)="chosenYearHandler($event)" (monthSelected)="chosenMonthHandler($event, datePicker)"></mat-datepicker>
is declared as in the Angular Material link, I add :
@ViewChild('datePicker') public dp;
ngAfterViewInit(): void {
this.dp.openedStream.subscribe(async _ => {
while (this.dp._popupComponentRef.instance._calendar == null)
await this.__delay__(100)
this.dp._popupComponentRef.instance._calendar._calendarHeaderPortal._attachedHost._attachedRef.instance.currentPeriodClicked = function () {
// redefine here calendar navigation in header
if (this.calendar.currentView == 'month')
this.calendar.currentView = 'year'
this.calendar.currentView = this.calendar.currentView == 'year' ? 'multi-year' : 'year';
}
})
}
That way you can customize the navigation from the header...
Solution 6:[6]
You can use these two handlers (chosenYearHandler & chosenMonthHandler) , and the formatting to show only month and year :
TS :
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import {MatDatepicker} from '@angular/material/datepicker';
// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment, Moment} from 'moment';
const moment = _rollupMoment || _moment;
// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
parse: {
dateInput: 'MM/YYYY',
},
display: {
dateInput: 'MM/YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
/** @title Datepicker emulating a Year and month picker */
@Component({
selector: 'datepicker-views-selection-example',
templateUrl: 'datepicker-views-selection-example.html',
styleUrls: ['datepicker-views-selection-example.css'],
providers: [
// `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
// application's root module. We provide it at the component level here, due to limitations of
// our example generation script.
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
export class DatepickerViewsSelectionExample {
date = new FormControl(moment());
chosenYearHandler(normalizedYear: Moment) {
const ctrlValue = this.date.value;
ctrlValue.year(normalizedYear.year());
this.date.setValue(ctrlValue);
}
chosenMonthHandler(normalizedMonth: Moment, datepicker: MatDatepicker<Moment>) {
const ctrlValue = this.date.value;
ctrlValue.month(normalizedMonth.month());
this.date.setValue(ctrlValue);
datepicker.close();
}
}
HTML:
<mat-form-field appearance="fill">
<mat-label>Month and Year</mat-label>
<input matInput [matDatepicker]="dp" [formControl]="date">
<mat-datepicker-toggle matSuffix [for]="dp"></mat-datepicker-toggle>
<mat-datepicker #dp
startView="multi-year"
(yearSelected)="chosenYearHandler($event)"
(monthSelected)="chosenMonthHandler($event, dp)"
panelClass="example-month-picker">
</mat-datepicker>
</mat-form-field>
CSS:
.example-month-picker .mat-calendar-period-button {
pointer-events: none;
}
.example-month-picker .mat-calendar-arrow {
display: none;
}
For more explanations : https://material.angular.io/components/datepicker/examples
Solution 7:[7]
In my case I hade a dynamic amount of inputs so I couldn't insert the data into the form control like in the documentation: https://material.angular.io/components/datepicker/overview#datepicker-views-selection
this was my solution:
setDateHandler(dateEvent: Moment,datepicker: MatDatepicker<any>) {
datepicker._selectedChanged.next(dateEvent);
datepicker.close(); }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | M M |
| Solution 2 | Indika Pradeep |
| Solution 3 | sean717 |
| Solution 4 | madhukar reddy |
| Solution 5 | |
| Solution 6 | Aymen TAGHLISSIA |
| Solution 7 |

