If you have a DateTime and you would like to get the localized name of the day according to the current culture, you can just do the following -
myDateTime.ToString(“ddd”);
I had a different scenario though, I had an object with a property of DayOfWeek enum which represents week days.
I needed to display just that property in my UI as a localized string.
In this case, I don’t have a DateTime so I can’t use its culture-based string formatting, so I can’t use the approach written above.
I could obviously generate a DateTime that is of such a day and use the that approach , but that seemed quite wasteful.
Well, fear not, there is a way to do that -
You can get the DateTimeFormatInfo in your current culture (be that the CurrentCulture or the CurrentUICulture), and retrieve the day name straight from there.
Code Snippet
DayOfWeek day = DayOfWeek.Monday;
System.Globalization.CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
string dayOfWeekLocalized = cultureInfo.DateTimeFormat.DayNames[(int)day];