Home > Mobile >  How to get localized "One Letter" weekday abbreviations in Dart?
How to get localized "One Letter" weekday abbreviations in Dart?

Time:05-03

If I do this

DateTime dateTime = DateTime(2022, 5, 1); // Sunday
print(DateFormat("E").format(dateTime));

Sun will be printed out to the console. Which is what I expected.

This also works if I change the system language.

How to printout the localized One Letter weekday abbreviation?

e.g. M T W T F S S

I thought I can get first letter using "substring", but it won't be correct for all languages. For example, Spanish weekdays are: Lunes, Martes, Miércoles, Jueves, Viernes, Sábado and Domingo, and first letter for "Miércoles" is X instead of M to difference it from "Martes".

In Java you could do something like this to get the One Letter abbreviation:

new SimpleDateFormat("EEEEE", Locale.getDefault());
simpleDateFormat.format(date);

For Spanish the output would be L M X J V S D

CodePudding user response:

Access only the first index of String

DateFormat.E(locale).format(date)[0]

CodePudding user response:

I found the solution to my question. Maybe its helpful to somebody.

var wd = DateFormat.EEEE().dateSymbols.NARROWWEEKDAYS;
print(wd);

The output for Spanish language [D, L, M, X, J, V, S]

This works with all languages i have tested (Japanese, German, English, Polish, Spanish, Italian etc.)

  • Related