Angular material date picker format locale

2019-08-19 12:03发布

问题:

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.

I have tried to include moment and the MomentDateAdapter module but have been unable to solve the issue.

Here is a stackblitz

I've set my providers like so:

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]));
}

回答1:

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},
  ],

If you use as provide of DateAdapter MomentDateAdapter, inject too MAT_DATE_LOCALE

{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]}

see official docs

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])

   }
 }

See example in stackblitz



回答2:

Change LOCALE_ID to MAT_DATE_LOCALE