如何在Angular2中设置DatePipe中的区域设置? [英] How to set locale in DatePipe in Angular2?

查看:239
本文介绍了如何在Angular2中设置DatePipe中的区域设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用欧洲格式 dd / mm / yyyy 显示日期,但使用DatePipe'shortDate'格式,它只显示使用美国日期样式 mm / dd / yyyy


我假设默认语言环境是en_US。也许我在文档中缺少,但是如何更改Angular2应用程序中的默认区域设置?或者也许有一些方法可以将自定义格式传递给DatePipe?

I want to display Date using European format dd/mm/yyyy but using DatePipe 'shortDate' format it only display using US date style mm/dd/yyyy.
Im assuming thats the default locale is en_US. Maybe I am missing in the docs but how can I change the default locale settings in an Angular2 app? Or maybe is there some way to pass a custom format to DatePipe?

推荐答案

从Angular2 RC6开始,您可以设置默认语言环境您的应用程序模块,通过添加一个提供者:

As of Angular2 RC6, you can set default locale in your app module, by adding a provider:

@NgModule({
  providers: [
    { provide: LOCALE_ID, useValue: "en-US" }, //replace "en-US" with your locale
    //otherProviders...
  ]
})

货币/日期/号码管道应该接收区域设置。 LOCALE_ID是从角度导入的 OpaqueToken / core。

The Currency/Date/Number pipes should pick up the locale. LOCALE_ID is an OpaqueToken, to be imported from angular/core.

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

对于更高级的用例,您可能需要从服务中选取语言环境。创建使用日期管道的组件时,区域设置将被解析(一次):

For a more advanced use case, you may want to pick up locale from a service. Locale will be resolved (once) when component using date pipe is created:

{
  provide: LOCALE_ID,
  deps: [SettingsService],      //some service handling global settings
  useFactory: (settingsService) => settingsService.getLanguage()  //returns locale string
}

希望它适用于您。

这篇关于如何在Angular2中设置DatePipe中的区域设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆