在您的html中,您可以使用(ngModelChange)=“functionName()”来触发任何更改日期的函数,并在您的ts中声明该函数 .
要更改日期的格式:
将其添加到 app.module.ts :
import{MatDateFormats, MAT_DATE_FORMATS, NativeDateAdapter, DateAdapter} from '@angular/material';
const MY_DATE_FORMATS = {
parse: {
dateInput: { day: 'numeric', month: 'numeric', year: 'numeric' }
},
display: {
dateInput: 'input',
monthYearLabel: { year: 'numeric', month: 'short' },
dateA11yLabel: { year: 'numeric', month: 'long', day: 'numeric' },
monthYearA11yLabel: { year: 'numeric', month: 'long' },
}
};
export class AppDateAdapter 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();
}
}
}
在 app.module.ts 的提供者中添加以下内容:
{provide: DateAdapter, useClass: AppDateAdapter},
{provide: MAT_DATE_FORMATS, useValue: MY_DATE_FORMATS}