Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Get the format, as string, used to display dates according to locale

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)

like image 652
Valentin Coudert Avatar asked Dec 07 '17 08:12

Valentin Coudert


People also ask

Which one of the following angular pipes formats a date value according to the locale settings?

The data transformation pipes use the LOCALE_ID token to format data based on rules of each locale. Formats a date value.

What is locale date format?

Java DateFormat The locale is used for specifying the region and language for making the code more locale to the user.

How do I set the date pipe locale?

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.


1 Answers

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 );
  }
}
like image 78
Simon Avatar answered Oct 21 '22 12:10

Simon