Open In App

Angular10 getLocaleMonthNames() Function

Last Updated : 04 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

The getLocaleMonthNames is used to get month of the year for the given locale.

Syntax:

getLocaleMonthNames(locale: string, formStyle: FormStyle, 
                         width: TranslationWidth):
                         ReadonlyArray<string>

NgModule: Module used by getLocaleMonthNames is:

  • CommonModule

Approach: 

  • Create the angular app to be used
  • In app.module.ts import LOCALE_ID because we need locale to be imported for using get getLocaleMonthNames.
import { LOCALE_ID, NgModule } from '@angular/core';
  • In app.component.ts import FormStyle,getLocaleMonthNames, TranslationWidth and LOCALE_ID
  • inject LOCALE_ID as a public variable and write the code for getting days of week using locale variable.
  • In app.component.html show the local variable using string interpolation
  • serve the angular app using ng serve to see the output.

Parameters:

  • locale: A locale code with rules.
  • formStyle: The form style in which days are to be shown
  • width: total width taken.

Return value :

  • array: an array of month names.

Example 1:

app.module.ts




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 { }


app.component.ts




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


app.component.html




<h1>
   GeeksforGeeks
</h1>
<p>Months of the year is: {{for}}</p>


Output:

Example 2:

app.module.ts




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 { }


app.component.ts




import {FormStyle,
        getLocaleMonthNames, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    day = getLocaleMonthNames(this.locale, 
    FormStyle.Standalone, 
    TranslationWidth.Wide);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){
            console.log(this.day)
        }
      }


Output:

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads