Cecil Boye Cecil Boye - 2 months ago 53x
TypeScript Question

How can i display date format based on [user location] in Typescript/Angular2

I have a project that displays date/time. I want to use a formatted date/time. But it should be depending on the browser/client language format and not a fixed format.

In the following post it uses a fixed format with pipes. This is NOT what I need!
"How to format date as dd/MM/yyyy in Angular 2 using pipes?"

Thank you..


I am using angular2localization for internationalisation. It seems to work well by setting a locale (which is stored once set though you would need to 'adjust' this initial yourself either by user action or config setting).

Node location

angular2localization documentation

It does use pipes but rather than hard coding uses a locale reference which is generated on startup.

The nice thing is that when a locale is changed it adjusts the settings instantly and caches the locale for use when the page is next opened. It is a client/user specific and not server override option (though you could force the locale if you so wished globally)

// 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);


Anyway hopefully that helps.