我需要帮助.我不知道如何更改material 2日期提货器的日期格式.我已经阅读了文档,但是我不知道我实际上需要做什么.Datepicker默认提供的输出日期格式为F.E.:6/9/2017
我试图实现的是将格式更改为2017年6月9日或任何其他日期.
文档https://material.angular.io/components/component/datepicker对我一点帮助都没有.
我需要帮助.我不知道如何更改material 2日期提货器的日期格式.我已经阅读了文档,但是我不知道我实际上需要做什么.Datepicker默认提供的输出日期格式为F.E.:6/9/2017
我试图实现的是将格式更改为2017年6月9日或任何其他日期.
文档https://material.angular.io/components/component/datepicker对我一点帮助都没有.
以下是我找到的唯一解决方案:
首先,创建常量:
const MY_DATE_FORMATS = {
parse: {
dateInput: {month: 'short', year: 'numeric', day: 'numeric'}
},
display: {
// dateInput: { month: 'short', year: 'numeric', day: 'numeric' },
dateInput: 'input',
monthYearLabel: {year: 'numeric', month: 'short'},
dateA11yLabel: {year: 'numeric', month: 'long', day: 'numeric'},
monthYearA11yLabel: {year: 'numeric', month: 'long'},
}
};
然后,您必须扩展NativeDateADapter:
export class MyDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat == "input") {
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
return this._to2digit(day) + '/' + this._to2digit(month) + '/' + year;
} else {
return date.toDateString();
}
}
private _to2digit(n: number) {
return ('00' + n).slice(-2);
}
}
In format function, you can choose whatever format you want
最后一步,您必须将其添加到模块提供程序中:
providers: [
{provide: DateAdapter, useClass: MyDateAdapter},
{provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS},
],
就这样.我不敢相信没有简单的方法可以通过@Input更改日期格式,但我们希望它能在material 2(目前为beta 6)的future 版本中实现.