Open In App

Angular10 getLocaleDateFormat() Function

In this article, we are going to see what is getLocaleDateFormat in Angular 10 and how to use it.

The getLocaleDateFormat is used to get the localized date-value formatting for the given locale.



Syntax:

getLocaleDateFormat(locale: string, width: FormatWidth): string

NgModule: Module used by getLocaleDateFormat is:



Approach: 

import { LOCALE_ID, NgModule } from '@angular/core';

Parameters:

Return value:

Example 1:




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule }
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }




import {FormStyle,
        getLocaleDateFormat, 
        TranslationWidth, 
        FormatWidth} 
        from '@angular/common';
  
import {Component, 
        Inject,OnInit, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    for = getLocaleDateFormat(this.locale,     
               FormatWidth.Short);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }




<h1>
   GeeksforGeeks
</h1>
<p>Date format is: {{for}}</p>

Output:

Example 2:




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule } 
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }




import {FormStyle,
  getLocaleDateFormat, 
  TranslationWidth, 
  FormatWidth} 
  from '@angular/common';
  
import {Component,
        Inject, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    for = getLocaleDateFormat(this.locale,     
            FormatWidth.Long);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }




<h1>
   GeeksforGeeks
</h1>
<p>Date format is: {{for}}</p>

Output:

Reference: https://angular.io/api/common/getLocaleDateFormat


Article Tags :