2016-08-18 52 views
0

我有一个显示日期/时间的项目。我想使用格式化的日期/时间。但它应该取决于浏览器/客户端语言格式,而不是固定的格式。我如何显示基于[用户位置]在Typescript/Angular2中的日期格式

在下面的文章中,它使用固定格式和管道。这不是我需要的! “How to format date as dd/MM/yyyy in Angular 2 using pipes?

谢谢。

+1

定义位置和格式。那么你需要找到用户位置,然后显示你定义的格式。 因为可能有希望以dd/mm/yyyy显示日期的美国用户的机会。 – Nitish

回答

1

我使用angular2localization实现国际化。通过设置一个区域设置(它被存储一次,虽然你需要通过用户操作或配置设置来“调整”这个初始值),但它似乎很好。

Node location

angular2localization documentation

它确实使用管道而不是硬编码使用其在启动时所产生的区域的参考。

好的是当一个语言环境发生变化时,它会立即调整设置并缓存区域设置以便在下次打开页面时使用。它是一个客户/用户特定的,而不是服务器覆盖选项(虽然你可能会迫使区域,如果你这样想全局)

// Sets a new locale & currency. 
selectLocale(language: string, country: string, currency: string, selectionText: string): void { 
    //Page will translate to new language automatically and without refreshing 
    //- the power of pipes... 
    this.locale.setCurrentLocale(language, country); 
    this.locale.setCurrentCurrency(currency); 

} 

反正希望帮助。

相关问题