Angular2 Material 使用 DD-MM-YYYY 格式的日期选择器问题,同时打开日期选择器弹出窗口
Angular2 Material Date picker issue using DD-MM-YYYY format, while opening Date picker popup
我已经应用了自定义日期适配器。但是打开选择器时将当前月份作为显示视图。
这是我的代码:
export class MyDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat === 'input') {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${day}-${month}-${year}`;
} else {
return date.toDateString();
}
}
}
const MY_DATE_FORMATS = {
parse: {
dateInput: 'dd-mm-YYYY',// { month: 'short', year: 'numeric', day: 'numeric' },
},
display: {
dateInput: 'input',
monthYearLabel: { year: 'numeric', month: 'numeric' },
dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
monthYearA11yLabel: { year: 'numeric', month: 'long' },
},
};
{ provide: DateAdapter, useClass: MyDateAdapter },
{ provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS },
您可以使用属性 [startAt]
和 [(ngModel)]
的绑定值强制日期选择器显示您选择的日期。
当您使用自定义日期格式时,material2 组件出现问题。
我已经应用了自定义日期适配器。但是打开选择器时将当前月份作为显示视图。
这是我的代码:
export class MyDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat === 'input') {
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
return `${day}-${month}-${year}`;
} else {
return date.toDateString();
}
}
}
const MY_DATE_FORMATS = {
parse: {
dateInput: 'dd-mm-YYYY',// { month: 'short', year: 'numeric', day: 'numeric' },
},
display: {
dateInput: 'input',
monthYearLabel: { year: 'numeric', month: 'numeric' },
dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
monthYearA11yLabel: { year: 'numeric', month: 'long' },
},
};
{ provide: DateAdapter, useClass: MyDateAdapter },
{ provide: MD_DATE_FORMATS, useValue: MY_DATE_FORMATS },
您可以使用属性 [startAt]
和 [(ngModel)]
的绑定值强制日期选择器显示您选择的日期。
当您使用自定义日期格式时,material2 组件出现问题。