C#: System.DayOfWeek output localized Weekday name
I have a plain DayOfWeek value and want to output the corresponding localized weekday name. Since it is an enum, there is no localization, says Microsoft. This is by design and will not be implemented in any way. I shall use DateTime’s .ToString(“dddd”). Alright, but I have no DateTime object from which I get the DayOfWeek. No problem. Here’s a workaround:
private string GetLocalizedWeekdayName(DayOfWeek d)
{
DateTime dt = DateTime.Today;
while (dt.DayOfWeek != d)
{
dt = dt.AddDays(1);
}
return dt.ToString("dddd");
}
In the comments Chris suggested a more straightforward approach. Though I never tried it I believe that it will do exactly the same:
private static string GetLocalizedWeekdayName(DayOfWeek weekday)
{
return CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)weekday];
}
Hey dude, that would work fine too:
foreach (var day in CultureInfo.CurrentCulture.DateTimeFormat.DayNames)
Console.WriteLine(day);
cheers
Or equivalent to your function….
private static string GetLocalizedWeekdayName(DayOfWeek weekday)
{
return CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)weekday];
}
best regards,
Chris
Hi Chris,
I like your approach better. Thanks!
Markus