Open In App

Angular10 getLocaleFirstDayOfWeek() Function

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see what is getLocaleFirstDayOfWeek in Angular 10 and how to use it. The getLocaleFirstDayOfWeek is used to get the first day of the week for the given locale.

Syntax:

getLocaleFirstDayOfWeek(locale : string): WeekDay

NgModule: Module used by FirstDayOfWeek is:

  • CommonModule

Approach: 

  • Create the angular app.
  • In app.module.ts, import LOCALE_ID because we need locale to be imported for using get getLocaleDayNames.
import { LOCALE_ID, NgModule } from '@angular/core';
  • In app.component.ts, import getLocaleFirstDayOfWeek and LOCALE_ID.
  • Inject LOCALE_ID as a public variable and write the code for getting first day 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.

Return value:

  • number: Index starting from 0.

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 {getLocaleFirstDayOfWeek } from '@angular/common';
  
import { Component, Inject,OnInit, LOCALE_ID } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    day = getLocaleFirstDayOfWeek(this.locale);
    dd = '';
    constructor(
        @Inject(LOCALE_ID) public locale: string,){
            if(this.day==0){
                this.dd = 'Sunday';
            }
        }
}


app.component.html




<h1>
  GeeksforGeeks
</h1>
  
<p>First Days of week : {{dd}}</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 {getLocaleFirstDayOfWeek } from '@angular/common';
  
import { Component, Inject, LOCALE_ID } from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    day = getLocaleFirstDayOfWeek(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){
        }
}


app.component.html




<h1>
  GeeksforGeeks
</h1>
  
<p>index value of first Day of week : {{day}}</p>


Output:

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



Last Updated : 30 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads