我有一个角度4项目,使用角度2材质Datepicker。我已将其中的日期格式更改为dd / mm / yyyy。我已将代码移动到npm包中并将其发布。当我测试包时,正确的日期格式正在工作,但当它与另一个项目集成时,Material datepicker正在选择默认格式(mm / dd / yyyy)。
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import * as _moment from 'moment';
/*Create date format DD/MM/YY */
const moment = _moment;
export const MY_FORMATS = {
parse: {
dateInput: 'D/MM/YYYY'
},
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MMMM Y',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM Y'
},
};
export class SomeModule {
static forRoot(): ModuleWithProviders{
return {
ngModule: SomeModule ,
providers: [
ReportingDashboardService,
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
}
}
}
答案 0 :(得分:0)
试试这个
import { DateAdapter } from '@angular/material';
constructor(private dateAdapter: DateAdapter<Date>) {
this.dateAdapter.setLocale('your locale name');
}