问题描述
我创建了一个有角材料日期选择器组件以与formly
一起使用,并尝试将其设置为使用GB作为区域设置,以便日期显示为26/07/2019
.但是,输入日期时,它仍解析为美国,从而导致显示无效的日期错误.
I have created an angular material date picker component to use with formly
, and tried to set it to use GB for the locale so that dates are shown as 26/07/2019
. However, when typing in the date, it parses as US still, resulting in invalid date errors being shown.
我尝试包括moment
和MomentDateAdapter
模块,但是无法解决问题.
I have tried to include moment
and the MomentDateAdapter
module but have been unable to solve the issue.
这是 stackblitz
我将提供者设置为:
providers: [
{ provide: LOCALE_ID, useValue: 'en-GB' },
{ provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE] },
{ provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS },
]
我的组件看起来像这样:
and my component looks like this:
import { Component, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';
import { FieldType } from '@ngx-formly/material';
import { MatInput } from '@angular/material';
import * as _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment} from 'moment';
const moment = _rollupMoment || _moment;
@Component({
selector: 'app-form-datepicker-type',
template: `
<input matInput
[errorStateMatcher]="errorStateMatcher"
[formControl]="formControl"
[matDatepicker]="picker"
[matDatepickerFilter]="to.datepickerOptions.filter"
[formlyAttributes]="field">
<ng-template #matSuffix>
<mat-datepicker-toggle [for]="picker"></mat-datepicker-toggle>
</ng-template>
<mat-datepicker #picker></mat-datepicker>
`,
})
export class DatepickerTypeComponent extends FieldType {
// Datepicker takes `Moment` objects instead of `Date` objects.
date = new FormControl(moment([2017, 0, 1]));
}
推荐答案
Sam,键"使用两个提供程序"MAT_DATE_FORMAT"和DateAdapter来解析/格式化值
Sam, the "key" are use two providers, "MAT_DATE_FORMAT" and DateAdapter to parse/format the values
providers: [
{provide: DateAdapter, useClass: CustomDateAdapter},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
],
如果您用作DateAdapter MomentDateAdapter的提供,则也要注入MAT_DATE_LOCALE
If you use as provide of DateAdapter MomentDateAdapter, inject too MAT_DATE_LOCALE
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]}
请参见官方文档
但是您也可以使用CustomDateAdapter,它是一个具有两种功能的类,格式和解析
But you can also use a CustomDateAdapter, that is a class with two functions, format and parse
export class CustomDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
let result=date.toDateString();
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
switch (displayFormat)
{
case 'DD/MM/YYYY':
// Return the format as per your requirement
result= `${day}-${month}-${year}`;
break;
default:
case 'MMM YYYY':
// Return the format as per your requirement
result= `${month}-${year}`;
break;
}
return result;
}
parse(value:string):any
{
let parts=value.split('/');
if (parts.length==3)
return new Date(+parts[2],(+parts[1])-1,+parts[0])
}
}
请参见 stackblitz中的示例
这篇关于角材料日期选择器格式区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!