getLocaleTimeFormat 用于获取给定语言环境的本地化 time-value 格式。
用法:
getLocaleTimeFormat(locale:string, width:FormatWidth):string
NgModule:getLocaleTimeFormat 使用的模块是:
- CommonModule
方法:
- 创建要使用的 angular 应用程序
- 在 app.module.ts 中导入 LOCALE_ID,因为我们需要导入 locale 以使用 getgetLocaleTimeFormat。
import { LOCALE_ID, NgModule } from '@angular/core';
- 在 app.component.ts 中导入 getLocaleTimeFormat 和 LOCALE_ID
- 将 LOCALE_ID 作为公共变量注入。
- 在 app.component.html 中使用字符串插值显示局部变量
- 使用 ng serve 为 angular 应用程序提供服务以查看输出。
参数:
- locale:包含带有规则的区域设置代码的字符串。
- width:可用于日期时间格式的字符串宽度。
返回值:
- string:本地化格式字符串的字符串。
范例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,){}
}
应用程序组件.html
<h1>
GeeksforGeeks
</h1>
<p>Time format is:{{for}}</p>
输出:
范例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,){}
}
应用程序组件.html
<h1>
GeeksforGeeks
</h1>
<p>Time format is:{{for}}</p>
输出:
参考: https://angular.io/api/common/getLocaleTimeFormat
相关用法
- AngularJS angular.isObject()用法及代码示例
- AngularJS angular.isUndefined()用法及代码示例
- AngularJS angular.isNumber()用法及代码示例
- AngularJS angular.isString()用法及代码示例
- AngularJS angular.isDefined()用法及代码示例
- AngularJS angular.isDate()用法及代码示例
- AngularJS angular.toJson()用法及代码示例
- AngularJS angular.element()用法及代码示例
- AngularJS angular.isFunction()用法及代码示例
- AngularJS angular.isElement()用法及代码示例
- AngularJS angular.equals()用法及代码示例
- AngularJS angular.isArray()用法及代码示例
- AngularJS angular.bind()用法及代码示例
- AngularJS angular.bootstrap()用法及代码示例
- AngularJS angular.forEach()用法及代码示例
- AngularJS angular.lowercase()用法及代码示例
- AngularJS angular.uppercase()用法及代码示例
注:本文由纯净天空筛选整理自taran910大神的英文原创作品 Angular 10 getLocaleTimeFormat() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。