简体   繁体   English

Datepicker Material Angular:如何显示月份的全名?

[英]Datepicker Material Angular: How can i display full name of the month?

I use the Datepicker component (Material) in my Angular application.我在我的 Angular 应用程序中使用了 Datepicker 组件(材料)。 In particularly i use the month view component (Here are an example in Stackblitz ).特别是我使用月视图组件(这是Stackblitz中的一个示例)。 How can i display the full name of the month?如何显示月份的全名? I want a result like: January, February, etc. Someone can help me?我想要这样的结果:一月、二月等。有人可以帮助我吗?

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:在你的 NgModule 中,像这样声明你的自定义格式:

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我更新了您在此处提供的 plunkr 示例

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.正如您在此处看到的, MatDateFormats类型仅允许自定义这 4 个显示属性。

Please check this content mat-datepicker-date-format请检查此内容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 https://stackblitz.com/angular/jejdvdkrbgl?file=src%2Fapp%2Fdatepicker-formats-example.ts

Change ts file as follow更改 ts 文件如下

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM