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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    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 ? – Midhun MP May 02 '22 at 18:16
  • 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. – nvoigt May 02 '22 at 18:33
  • @MidhunMP I can get "Sun Mon Tue Wed Thu Fri Sat" but i need "S M T W T F S" – Somjit Glin-Jan May 02 '22 at 18:34
  • 1
    @nvoigt As i wrote in the question above. You can do "new SimpleDateFormat("EEEEE", Locale.getDefault());" in Java and it works. – Somjit Glin-Jan May 02 '22 at 18:42
  • 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 enough – Alvaro Carrasco May 02 '22 at 18:44
  • @AlvaroCarrasco Thanks. But this works only for some languages like english or german, but not for Spanish – Somjit Glin-Jan May 02 '22 at 18:55
  • @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 moment – Midhun MP May 02 '22 at 19:05

2 Answers2

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

2

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

igorz
  • 141
  • 2
  • 3