DCSIMG
Localize DayOfWeek Enum - Zuker On Foundations

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)
Localize DayOfWeek Enum

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];

Published Tuesday, October 12, 2010 7:15 PM by Amir Zuker

תגים:,

Comments

# re: Localize DayOfWeek Enum@ Tuesday, October 12, 2010 10:43 PM

replace "ci" with "cultureInfo".

Yitzhak Steinmetz

# re: Localize DayOfWeek Enum@ Tuesday, October 12, 2010 11:26 PM

Thanks for bringing that up Yitzhak

Amir Zuker

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: