Reputation: 1919
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)
Upvotes: 12
Views: 14306
Reputation: 3264
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 );
}
}
Upvotes: 21
Reputation: 52847
If you want to declaratively format a date within your template:
{{ today | date: 'dd/MM/yyyy' }}
If you want to imperatively get the formatted date within code:
import { DatePipe } from '@angular/common';
// in your component class
var date = new DatePipe().transform(today, 'dd/MM/yyyy')
Upvotes: 2