热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

angular10getlocaleedateformat()函数

angular10getlocaleedateformat()函数

angular 10 getlocaleedateformat()函数

原文:https://www . geeksforgeeks . org/angular 10-getlocaledateformat-function/

在本文中,我们将看到 Angular 10 中什么是getlocaleedateformat以及如何使用它。

getLocaleDateFormat 用于获取给定地区的本地化日期值格式。

语法:

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

模块:GetLocaleDateformat 使用的模块是:


  • 公共模块

进场:


  • 创建要使用的角度应用程序

  • 在 app.module.ts 中导入 LOCALE_ID,因为我们需要使用 getLocaleDateFormat 导入区域设置。

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


  • 在 app.component.ts 中,导入 getLocaleDateFormat 和 LOCALE_ID

  • 将 LOCALE_ID 作为公共变量注入。

  • 在 app.component.html,使用字符串插值显示局部变量

  • 使用 ng serve 为 angular app 服务,以查看输出。

参数:


  • 区域设置:包含带有规则的区域设置代码的字符串。

  • 宽度:格式类型。

返回值:


  • 字符串:本地化格式字符串的字符串。

例 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,
        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,){}
      }

app.component.html


   GeeksforGeeks


Date format is: {{for}}


输出:

例 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,
  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,){}
      }

app.component.html


   GeeksforGeeks


Date format is: {{for}}


输出:

参考资料:【https://angular . io/API/common/getlocalateformat】


推荐阅读
author-avatar
_快樂Smile_903
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有