我在我的https://material.angular.io/components/component/datepicker项目中使用了angular4。如何在输入框中设置选定日期的格式,如“2017年5月29日”。目前显示为29/05/2017
样本代码:
<md-input-container>
<input mdInput readonly="true" [mdDatepicker]="startDatePicker" [mdDatepickerFilter]="myFilter" [min]="minDate" [max]="maxDate" class="date-text ng-dirty ng-valid-parse ng-touched ng-not-empty ng-valid ng-valid-required" placeholder="Choose a date">
<button mdSuffix [mdDatepickerToggle]="startDatePicker" class="date-icon"></button>
</md-input-container>
<md-datepicker #startDatePicker [dateFormat]="'LL'" startView="month" [startAt]="currentDate"></md-datepicker>TS:
@ViewChild('startDatePicker') startDatePicker: MdDatepicker<Date>;PS。DateFormat=“LL”不起作用
发布于 2017-06-09 10:02:08
您现在不能轻松地设置日期格式。在Matter2Datepicker API接口中没有这样的属性,更改格式的唯一正确方法是提供您自己的DateAdapter实现。官方文件没有提供任何关于这方面的说明。现在只有一个基本的实施,它根据语言环境定义格式。对此有很多抱怨,很快就会有更多的适配器。您可以尝试使用一个基于moment.js的这问题线程,或者只是等待。
您还可以使用区域设置来获取数据报警器中的不同格式。但这只是一个解决办法,直到一个适当的官方解决方案出现:
export class AppModule {
constructor(private dateAdapter:DateAdapter<Date>) {
dateAdapter.setLocale('de'); // DD.MM.YYYY
}
}发布于 2017-09-11 13:04:01
为此,我更愿意使用模块提供程序:
@NgModule({
...
providers: [
{ provide: LOCALE_ID, useValue: navigator.language }
...
]
})
export class AppModule { }或者,您可以用“de”替换navigator.language,以实现与上面相同的功能。
https://stackoverflow.com/questions/44238116
复制相似问题