Angular 10 getLocaleTimeFormat() Function
Last Updated :
30 Apr, 2021
The getLocaleTimeFormat is used to get the localized time-value formatting for the given locale.
Syntax:
getLocaleTimeFormat(locale: string, width: FormatWidth): string
NgModule: Module used by getLocaleTimeFormat is:
Approach:
Parameters:
- locale: A string containing locale code with rules.
- width: String widths available for date-time formats.
Return value:
- string: string of localized formatting strings.
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,
getLocaleTimeFormat, 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 = getLocaleTimeFormat( this .locale, FormatWidth.Short);
constructor(
@Inject(LOCALE_ID) public locale: string,){}
}
|
app.component.html
< h1 >
GeeksforGeeks
</ h1 >
< p >Time format 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,
getLocaleTimeFormat, 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 = getLocaleTimeFormat( this .locale, FormatWidth.Full);
constructor(
@Inject(LOCALE_ID) public locale: string,){}
}
|
app.component.html
< h1 >
GeeksforGeeks
</ h1 >
< p >Time format is: {{for}}</ p >
|
Output:
Reference: https://angular.io/api/common/getLocaleTimeFormat
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...