1

I wonder if there is a native C# way to parse different strings containing the name of the week in different languages.

For example I have:

"Mo" or "Mon" for monday.

and this string parsed should return day 1 (monday) But in the same input I could have

"Lu" or "Lun" (italian)

And the output should be as well day 1 (monday).

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • 1
    `DayOfWeek` is your enum. Potentially, if you provide the actual date. Any date with specific day of the week, you can extract a language-specific name from this date, using specific culture, e.g. `fr-FR, de-DE, it-IT`. `DateTime.ToString("dddd")` or `"ddd"` gets you that. Use it with different culture, it gets you in any language – T.S. Jul 21 '21 at 21:53
  • 3
    What are your constraints? What would your ideal code look like? Do you know the language up front? What languages are we talking about? How do you handle languages that are substantially similar (for example the romance languages)? – Flydog57 Jul 21 '21 at 21:54
  • my languages are in a range of possible 7-8 languages. the input strings are either the day full name or a shortened version 2 or 3 letters. – Claudio Ferraro Jul 21 '21 at 21:58
  • you can preload matrix of 7 arbitrary continous dates in all languages you need and then translate at will using the table – T.S. Jul 21 '21 at 21:59
  • i don't have a real date. the days are opening hours from companies – Claudio Ferraro Jul 21 '21 at 22:00
  • 2
    Great question. I'm not finding any references about a straightforward way to do this. `CultureInfo`'s `DateTimeFormat` property has `DayNames` and `AbreviatedDayNames` properties that you could probably do an `IndexOf` on, and then cast the result to a `DayOfWeek`. – StriplingWarrior Jul 21 '21 at 22:00
  • you should edit your question, starting from what you have, what you want to do with it and what output should look like – T.S. Jul 21 '21 at 22:02
  • Side note: Monday is not necessary first day of the week... But as @StriplingWarrior said all that information is present in CultureInfo object for locale you are looking for. Note on note - locale is not the same as language, so what day of the week is Monday and what names for days of the week are used is locale (country+language) specific and not necessary language specific. – Alexei Levenkov Jul 21 '21 at 22:13
  • Unlike with [month names](https://stackoverflow.com/a/258828/11683), a date consisting of just a weekday name cannot be parsed because it's not going to be [specific enough](https://stackoverflow.com/a/61521426/11683). And if you artificially extend your string to include an actual date for `ParseExact` to consume, that date must be the correct date on which your potential weekday name occurred, otherwise there will be the same 'incorrect format' exception again. So yes, analyzing the day names from `DateTimeFormat` is the way. – GSerg Jul 21 '21 at 22:16
  • The Spanish and Italian (for example) abbreviated Day names are often the same. The months names, too. French also, if someone forgets to add a dot. Not so simple, if your Cultures are among these. – Jimi Jul 21 '21 at 22:56
  • Yeah, these kinds of problems usually start with the assumption that you know the culture for the given context, either based on user settings or client information or something like that. Without knowing that context, things become error-prone. The problem is not where multiple cultures have the same day names for the same days: it's when they have the same day names for different days. For example, "Sab" means Saturday in some cultures and Sunday in others. – StriplingWarrior Jul 21 '21 at 23:18

0 Answers0