bokkie
bokkie

Reputation: 1590

Angular custom date format adapted to locale

I need to display dates based on locale. All goes well when I use the predefined formats (https://angular.io/api/common/DatePipe#pre-defined-format-options)

import { formatDate } from '@angular/common';
...
//if `format` is one from the predefined ones, for example 'shortDate', it works fine
return formatDate(value, format, this.localeService.currentLocale);

But I need to display only months and years, format which doesn't exist in the predefined ones, therefor I need to create a custom format and give it as a parameter to the formatDate function. BUT. if I say format = 'MM/YYYY', this will be the same for all languages. So I need it adapted to each locale, for example Germany has 'MM.YYYY', but maybe there are countries that have "YYYY-MM". And I would really like to avoid doing a switch on the locale, and for each currently used locale, set a different format. Then for a new language add a new one. So, is there a way to say that I want the format to contain month and year, but to be adapted to the locale?

Upvotes: 6

Views: 892

Answers (0)

Related Questions