简体   繁体   中英

Angular 2 material Datepicker changed date format is not working

I have an angular 4 project which is using angular 2 material Datepicker. I have changed the date format in it to dd/mm/yyyy. I have moved the code into a npm package and published it. The correct date format is working when I test the package but when its integrated with another project, Material datepicker is picking default format(mm/dd/yyyy).

import {MomentDateAdapter} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import * as _moment from 'moment';

/*Create date format DD/MM/YY */
const moment =  _moment;
export const MY_FORMATS = {
  parse: {
    dateInput: 'D/MM/YYYY'
  },
  display: {
    dateInput: 'DD/MM/YYYY',
    monthYearLabel: 'MMMM Y',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM Y'
  },
};

export class SomeModule {
  static forRoot(): ModuleWithProviders{
    return {
      ngModule: SomeModule ,
      providers: [
        ReportingDashboardService,
        {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
        {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
      ]
    }
  }
}

try this

import { DateAdapter } from '@angular/material';

constructor(private dateAdapter: DateAdapter<Date>) {
    this.dateAdapter.setLocale('your locale name');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM