问题描述
我想使用欧洲格式 dd / mm / yyyy
显示日期,但是使用DatePipe shortDate 格式,它只显示美国日期样式毫米/日/年
。
我假设默认语言环境是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
.
I'm 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是从角度导入的 / 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
}
希望它适合您。
这篇关于如何在Angular 2中的DatePipe中设置区域设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!