如何更改角度材质的日期选择器格式 [英] How to change angular material datepicker format
问题描述
这是我使用角形材质datepicker时获得的日期格式..Wed Nov 21 2018 00:00:00 GMT+0530 (India Standard Time)
This is the format of date I am getting when I am using angular material datepicker....Wed Nov 21 2018 00:00:00 GMT+0530 (India Standard Time)
但是我需要使用(YYYY-MM-DD)
或(YYYY-MM-DDTHH:mm)
这种格式的日期.
But I need date in (YYYY-MM-DD)
or (YYYY-MM-DDTHH:mm)
this format.
这是我用来从角形材料形式捕获数据的模型类
this is model class I am using to capture data from angular material form
export class FlightSchedule {
constructor(
public destination: string,
public origin: string,
public date: string,
public limit: string
) {}
}
请帮助我,我无法将日期转换为YYYY-MM-DD
或YYYY-MM-DDTHH:mm
格式.
Please help me, I am unable to convert date in YYYY-MM-DD
or YYYY-MM-DDTHH:mm
format.
我是Angular的新手
I am new to Angular
预先感谢
推荐答案
您需要提供一个包含您要使用的格式的对象.该对象应类似于:
You need to provide an object containing the formats you wish to use. The object should look something like:
export const MY_FORMATS = {
parse: {
dateInput: 'LL',
},
display: {
dateInput: 'YYYY-MM-DD',
monthYearLabel: 'YYYY',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'YYYY',
},
};
然后,您需要将其添加到您的provider数组中,如下所示:
You then need to add that in to your providers array, like so:
import { MAT_DATE_FORMATS } from '@angular/material';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
//...
providers: [
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
Here is a StackBlitz demo to show it working
这篇关于如何更改角度材质的日期选择器格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!