Search code examples
angularjslocaleangularjs-filter

How to get format strings from ngLocale


Is there a possibility to get format strings from the ngLocale module? I need this strings for an own filter which works with moment.js-objects and moment-timezone.

For example in en-us the "short"-String is "M/d/yy h:mm a", for de-de it is "dd.MM.yy HH:mm" ...

Is there any way to get this strings without the "date"-Filter?


Solution

  • Sure, you can use the $locale service for this. While the only documented API of this service is $location.id, the actual service is just a JavaScript object coming from the locale file, ex.: https://code.angularjs.org/snapshot/i18n/angular-locale_en-us.js

    Using your example, you could get access to the short data format string by injecting the $locale service and doing: $locale.DATETIME_FORMATS.short.

    Check the mentioned locale file to see what other locale-specific values are available. And yes, I agree that those are very useful in custom filters / directives.