问题描述
这是我使用角材料日期选择器时得到的日期格式....2018 年 11 月 21 日星期三 00:00:00 GMT+0530(印度标准时间)
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',
},
};
然后您需要将其添加到您的 providers 数组中,如下所示:
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},
],
这篇关于如何更改角度材料日期选择器格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!