close

How to set locale in DatePipe in Angular 2?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to set locale in DatePipe in Angular 2? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to set locale in DatePipe in Angular 2 ?

  1. How to set locale in DatePipe in Angular 2 ?

    to set locale in DatePipe in Angular 2 I was struggling with the same issue and didn't work for me using this
    {{dateObj | date:'ydM'}}
    So, I've tried a workaround, not the best solution but it worked:
    {{dateObj | date:'d'}}/{{dateObj | date:'M'}}/{{dateObj | date:'y'}}
    I can always create a custom pipe.

  2. How to set locale in DatePipe in Angular 2 ?

    to set locale in DatePipe in Angular 2 I was struggling with the same issue and didn't work for me using this
    {{dateObj | date:'ydM'}}
    So, I've tried a workaround, not the best solution but it worked:
    {{dateObj | date:'d'}}/{{dateObj | date:'M'}}/{{dateObj | date:'y'}}
    I can always create a custom pipe.

Method 1

With angular5 the above answer no longer works!

The following code:

app.module.ts

@NgModule({
  providers: [
    { provide: LOCALE_ID, useValue: "de-at" }, //replace "de-at" with your locale
    //otherProviders...
  ]
})

Leads to following error:

Error: Missing locale data for the locale “de-at”.

With angular5 you have to load and register the used locale file on your own.

app.module.ts

import { NgModule, LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localeDeAt from '@angular/common/locales/de-at';

registerLocaleData(localeDeAt);

@NgModule({
  providers: [
    { provide: LOCALE_ID, useValue: "de-at" }, //replace "de-at" with your locale
    //otherProviders...
  ]
})

Method 2

I was struggling with the same issue and didn’t work for me using this

{{dateObj | date:'ydM'}}

So, I’ve tried a workaround, not the best solution but it worked:

{{dateObj | date:'d'}}/{{dateObj | date:'M'}}/{{dateObj | date:'y'}}

I can always create a custom pipe.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment