Angular Material 5 - 如何在 mat-datepicker 中自定义日期
Angular Material 5 - How to Customise date in mat-datepicker
如何自定义 MM/DD/YYYY 格式的 mat-datepicker 日期?
我正在使用以下配置:
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>
TS:
import {Component} from '@angular/core';
/** @title Basic datepicker */
@Component({
selector: 'datepicker-overview-example',
templateUrl: 'datepicker-overview-example.html',
styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {}
当我打印表单值时,它给我以下输出:
Sun Dec 31 2017 00:00:00 GMT+0530 (IST)
我希望格式为 MM/DD/YYYY。
我试过这个配置:https://material.angular.io/components/datepicker/overview#customizing-the-parse-and-display-formats
但这对我不起作用。我正在使用默认的 MatNativeDateModule(不是 moment js)
您可以按如下方式使用日期管道,
<input mdInput placeholder="Title" [ngModel]="mydate | date:'MM-dd-yyyy'">
下面的代码将格式化您 angular 应用程序中所有日期选择器的日期。
为日期格式创建常量。
export const DateFormat = {
parse: {
dateInput: 'input',
},
display: {
dateInput: 'MM/DD/YYYY',
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'MM/DD/YYYY',
monthYearA11yLabel: 'MMMM YYYY',
}
};
并在应用模块中使用以下代码
providers: [
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
{ provide: MAT_DATE_FORMATS, useValue: DateFormat }
]
如何自定义 MM/DD/YYYY 格式的 mat-datepicker 日期?
我正在使用以下配置:
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>
TS:
import {Component} from '@angular/core';
/** @title Basic datepicker */
@Component({
selector: 'datepicker-overview-example',
templateUrl: 'datepicker-overview-example.html',
styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {}
当我打印表单值时,它给我以下输出:
Sun Dec 31 2017 00:00:00 GMT+0530 (IST)
我希望格式为 MM/DD/YYYY。
我试过这个配置:https://material.angular.io/components/datepicker/overview#customizing-the-parse-and-display-formats
但这对我不起作用。我正在使用默认的 MatNativeDateModule(不是 moment js)
您可以按如下方式使用日期管道,
<input mdInput placeholder="Title" [ngModel]="mydate | date:'MM-dd-yyyy'">
下面的代码将格式化您 angular 应用程序中所有日期选择器的日期。
为日期格式创建常量。
export const DateFormat = {
parse: {
dateInput: 'input',
},
display: {
dateInput: 'MM/DD/YYYY',
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'MM/DD/YYYY',
monthYearA11yLabel: 'MMMM YYYY',
}
};
并在应用模块中使用以下代码
providers: [
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
{ provide: MAT_DATE_FORMATS, useValue: DateFormat }
]