39

How to Change language of Datepicker of Material Angular? I can't find in documentation for Angular material 2. Here is a plunkr https://plnkr.co/edit/unzlijtsHf3CPW4oL7bl?p=preview

<md-input-container>
  <input mdInput [mdDatepicker]="picker" placeholder="Choose a date">
  <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>
<md-datepicker #picker></md-datepicker>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Melchia
  • 22,578
  • 22
  • 103
  • 117
  • 4
    Are tryed to use `momentjs`? In your `main.ts` you can provide a locale settings by `{ provide: LOCALE_ID, useValue: 'fr-FR' }`. You should import your locale by `import 'moment/locale/fr` and after you can set locale in your datepicker component to `ngOnInit`-hook by `moment.locale('de');`. In your datepicker component you should also import moment by `import * as moment from 'moment';` – Gregor Doroschenko Aug 17 '17 at 05:14
  • 1
    Here is a link to momentjs documentation https://momentjs.com/docs/ – Gregor Doroschenko Aug 17 '17 at 05:18
  • 1
    Nice idea indeed by setting moment.locale('fr'); it should work. – Melchia Aug 17 '17 at 05:19

6 Answers6

69
import {MAT_DATE_LOCALE} from '@angular/material';

&

providers: [{ provide: MAT_DATE_LOCALE, useValue: 'es-ES' }]

Do this in tpv.modules.ts

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
vladernn
  • 835
  • 1
  • 6
  • 7
  • Welcome to StackOverflow! This has the potential to be a good answer - please format your code and add your comment to the actual answer (you can edit it). :) – Shadow May 17 '18 at 11:37
  • @antione129 Thanks, I tryed to edit it, but I'm new and I don't know how. – vladernn May 17 '18 at 15:45
  • There is an edit link directly beneath the answer (look for some grey text, or if you really struggle. `CTRL+F` will be your ally. Someone has helped you by putting your code into blocks for you, but adding the comment to the answer would still be valuable. – Shadow May 17 '18 at 23:12
  • 10
    This will work for the mat-datepicker UI, but this does correctly change the validation for the input field. For example, selecting Christmas on the UI will display 25/12/2018, but when you start to edit the input field, it will invalidate the form unless it's then changed to 12/25/2018. – Maximillion Bartango Apr 16 '19 at 22:01
  • 1
    Potentially might need to import from '@angular/material/core' if it cannot find it in the material folder. (https://stackoverflow.com/a/61352498/4997466) – Marty131 Mar 11 '21 at 00:54
24

md-datepicker provides setLocale method which can be used to set any language (list of locale).

Here's an example of setting locale to 'fr':

export class DatepickerOverviewExample {
  
  constructor(private dateAdapter: DateAdapter<Date>) {
    this.dateAdapter.setLocale('fr');   
  }
  
}

One thing to keep in mind, md-datepicker's default date parsing format is mm/dd/yyyy, so if a locale has a different date format (for 'fr' its dd/mm/yyyy), we will need to define a class that extends NativeDateAdapter to parse the new date format. Without setting the proper date format, there will be an issue like this question.

import { NativeDateAdapter, DateAdapter, MD_DATE_FORMATS } from "@angular/material/core";
    
export class FrenchDateAdapter extends NativeDateAdapter {
  parse(value: any): Date | null {
    if ((typeof value === 'string') && (value.indexOf('/') > -1)) {
      const str = value.split('/');
      if (str.length < 2 || isNaN(+str[0]) || isNaN(+str[1]) || isNaN(+str[2])) {
        return null;
      }
      return new Date(Number(str[2]), Number(str[1]) - 1, Number(str[0]), 12);
    }
    const timestamp = typeof value === 'number' ? value : Date.parse(value);
    return isNaN(timestamp) ? null : new Date(timestamp);
  }
}

@Component({
  ...
  providers: [{provide: DateAdapter, useClass: FrenchDateAdapter}],
})

Plunker demo

Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78
Nehal
  • 13,130
  • 4
  • 43
  • 59
  • How to get the export const MY_FORMATS = { parse: { dateInput: 'LL', }, display: { dateInput: 'ddd, MMM. D YYYY', monthYearLabel: 'MMM YYYY', dateA11yLabel: 'LL', monthYearA11yLabel: 'MMMM YYYY', }, }; ? – app Nov 11 '20 at 20:23
13

Locale setting can be provided via MAT_DATE_LOCALE constant, but to change language dynamically you need to use DateAdapter as it is shown in https://material.angular.io/components/datepicker/overview#internationalization

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {

  constructor(private dateAdapter: DateAdapter<any>) {}

  setFrench() {
    // Set language of Datepicker
    this.dateAdapter.setLocale('fr');
  }

}

Here is another example when you need to configure locale from external script:

<script>
  window.appConfig = {
    language: 'fr',
    // Other config options
    // ...
  };
<script>
<app-root></app-root>

In this case you should also use DateAdapter:

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

declare let window: any;

@Injectable()
export class AppConfigService {

  appConfig = window.appConfig;

  constructor(private dateAdapter: DateAdapter<any>) {
    // Set language of Datepicker
    this.dateAdapter.setLocale(this.appConfig.language);
  }

}
user147677
  • 471
  • 4
  • 7
4

For anyone who has a bug when editing input field (eg: putting DD/MM/YYYY will change it to MM/DD/YYYY or simply not working) create an adapter:

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

export class FrenchDateProvider extends NativeDateAdapter {

   parse(value: string) {
      let it = value.split('/');
      if (it.length == 3) {
         return new Date(+it[2], +it[1] - 1, +it[0], 12);
      }
   }

   format(date: Date, displayFormat: Object) {
      return ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear();
   }

}

Add it to your module as provider:

@NgModule({
   providers: [
      { provide: DateAdapter, useClass: FrenchDateProvider }
   ]
})
export class SharedModule { }
Emeric
  • 6,315
  • 2
  • 41
  • 54
  • How to format for this setting?How to get the export const MY_FORMATS = { parse: { dateInput: 'LL', }, display: { dateInput: 'ddd, MMM. D YYYY', monthYearLabel: 'MMM YYYY', dateA11yLabel: 'LL', monthYearA11yLabel: 'MMMM YYYY', }, }; – app Nov 11 '20 at 20:29
1

For me the working solution is similar to vladernn's, however it should be:

import {MAT_DATE_LOCALE} from '@angular/material/core';

and

providers: [{ provide: MAT_DATE_LOCALE, useValue: 'pl' }]

in material.module.ts file.

Difference: new import address and shorter useValue code.

Bartosz Petryński
  • 1,024
  • 8
  • 8
0

I tried this and worked for me

 constructor(private _adapter: DateAdapter<any>, private translate:TranslateService ) {
 
  this._adapter.setLocale(this.translate.getBrowserLang());

 }
General Grievance
  • 4,555
  • 31
  • 31
  • 45