Localize DayOfWeek Enum

October 12, 2010

tags: ,
2 comments

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

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. Yitzhak SteinmetzOctober 12, 2010 ב 22:43

    replace “ci” with “cultureInfo”.

    Reply
  2. Amir ZukerOctober 12, 2010 ב 23:26

    Thanks for bringing that up Yitzhak

    Reply