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 ?
相关问题
- Angular RxJS mergeMap types
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
As of Angular2 RC6, you can set default locale in your app module, by adding a provider:
The Currency/Date/Number pipes should pick up the locale. LOCALE_ID is an OpaqueToken, to be imported 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:
Hope it works for you.
You do something like this:
{{ dateObj | date:'shortDate' }}
or
{{ dateObj | date:'ddmmy' }}
See: https://angular.io/docs/ts/latest/api/common/index/DatePipe-pipe.html
For those having problems with AOT, you need to do it a little differently with a useFactory:
Solution with LOCALE_ID is great if you want to set the language for your app once. But it doesn’t work, if you want to change the language during runtime. For this case you can implement custom date pipe.
Now if you change the app display language using TranslateService (see ngx-translate)
the formats within your app should automatically being updated.
Example of use:
or check my simple "Notes" project here.
I was struggling with the same issue and didn't work for me using this
So, I've tried a workaround, not the best solution but it worked:
I can always create a custom pipe.
Ok, I propose this solution, very simple, using
ngx-translate