I use the Datepicker component (Material) in my Angular application. In particularly i use the month view component (Here are an example in Stackblitz). How can i display the full name of the month? I want a result like: January, February, etc. Someone can help me?
4 Answers
you can import
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
in your NgModule, declare your custom formats like this:
const MY_FORMATS = {
parse: { dateInput: 'DD/MM/YYYY' },
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'DD/MM/YYYY',
monthYearA11yLabel: 'MM YYYY',
}
}
And then provide them in the import section of your module like this:
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
This will show the full month in the upper left corner of the datepicker.
I updated the plunkr example you provided here
Unfortunately there is no way to further customize the dates shown in the datepicker. As you can see here the MatDateFormats type only allows the customization of this 4 display properties.
Please check this content mat-datepicker-date-format
Are you looking for this date formatting stuff? or if not, please let me know the exact place where you need the change.
You can use moment date formats to do that
// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'LL',
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM YYYY',
},
};
Full example
https://stackblitz.com/angular/jejdvdkrbgl?file=src%2Fapp%2Fdatepicker-formats-example.ts
Change ts file as follow
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
export const MY_FORMATS = {
display: {
dateInput: 'LL'
},
};
@Component({
selector: 'datepicker-start-view-example',
templateUrl: 'datepicker-start-view-example.html',
styleUrls: ['datepicker-start-view-example.css'],
providers: [
{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
})
export class DatepickerStartViewExample {
startDate = new Date(2020, 0, 1);
}