How can I get the format, as a string, used by Angular to display dates ? I use the date
pipe after setting LOCALE_ID
in my app.module
.
Now I'd like to retrieve the "dd/mm/yyyy" string, to put in my input placeholders.
Basically, what's described here but for Angular.
Thanks
[edit]
To make it more explicit, I'm not looking for a way to format dates, that's already done (with the native date
pipe). I want the string that represents the format being used by this pipe.
Because I'd like my datepicker placeholder to be like
"Date (format: XXXXXX)"
and I want the "XXXXXX" part to be correct (i.e. "jj/mm/aaaa" for french, "mm/dd/yyyy" for english)
The data transformation pipes use the LOCALE_ID token to format data based on rules of each locale. Formats a date value.
Java DateFormat The locale is used for specifying the region and language for making the code more locale to the user.
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... ] }) The Currency/Date/Number pipes should pick up the locale.
I hope that in the meantime you've come across getLocaleDateFormat() and getLocaleTimeFormat(). (Angular 6)
import { getLocaleDateFormat, FormatWidth } from '@angular/common';
export class Foobar {
constructor( @Inject( LOCALE_ID ) private locale: string ) { }
private getDateFormat() {
return getLocaleDateFormat( this.locale, FormatWidth.Short );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With