1

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..

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
C.B.
  • 310
  • 1
  • 2
  • 18
  • 1
    Define location and format. So then you need to just find user location and then show format which you have defined. Because there might be chances that American user who wants to see date in dd/mm/yyyy. – Nitish Aug 18 '16 at 07:47

2 Answers2

3

I have found a much simpler way! It just happened that i stumbled on this solution after a month.

var d = new Date();

var n = d.toLocaleDateString();

Source: http://www.w3schools.com/jsref/jsref_tolocaledatestring.asp

C.B.
  • 310
  • 1
  • 2
  • 18
1

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);
    this.locale.setCurrentCurrency(currency);

}

Anyway hopefully that helps.

Matrim
  • 645
  • 7
  • 13