How to get localized "One Letter" weekday abbreviations in Dart?
Asked Answered
I

2

5

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

Ineffaceable answered 2/5, 2022 at 17:59 Comment(7)
One letter abbreviation? Could you please explain a little further and what's the problem you are facing with above code? Isn't it giving you the localised value based on phone's locale ?Ferneferneau
Are you sure there are gobally recognized 1-char weekday abbreviations? I never heard about it and upon googling it found multiple one's even in the US only in use with universities.Dysgraphia
@MidhunMP I can get "Sun Mon Tue Wed Thu Fri Sat" but i need "S M T W T F S"Ineffaceable
@Dysgraphia As i wrote in the question above. You can do "new SimpleDateFormat("EEEEE", Locale.getDefault());" in Java and it works.Ineffaceable
I don't think there's a standard way to do this in dart. You can prob do this: DateFormat("E").format(dateTime)[0] but not sure if that is good enoughSamoyedic
@AlvaroCarrasco Thanks. But this works only for some languages like english or german, but not for SpanishIneffaceable
@SomjitGlin-Jan I don't think there is a built-in function available, also didn't find anything on intl package. All you can do is to create a map which will hold the 3 letter abbreviation as key and 1 letter abbreviation as value. It would be tedious task to handle that for all the supported language, but I don't think there is any other way at the momentFerneferneau
C
6

Depending on a use case, like a calendar header, it might be actually better to use standalone weekday format.

One-letter standalone format is supported with custom pattern ccccc:

var dateTime = DateTime(2022, 5, 1); // Sunday

for (int i = 0; i < 7; i++) {
  print(DateFormat("ccccc").format(dateTime));

  dateTime = dateTime.add(Duration(days: 1));
}

That prints: S M T W T F S

Cabinda answered 14/2, 2023 at 5:22 Comment(0)
I
3

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.)

Ineffaceable answered 3/5, 2022 at 6:6 Comment(1)
I think you can shorten this to just DateFormat().dateSymbols.NARROWWEEKDAYS;Franconia

© 2022 - 2024 — McMap. All rights reserved.