Search code examples
c#culturedayofweek

C# Cultures: Localize DayOfWeek?


How do I localize a DayOfWeek other than today?
The following works just fine for the current day:

DateTime.Now.ToString("dddd", new CultureInfo("it-IT"));

But how can I localize all days of the week??

Edit: A possible (and probably very, very wrong) solution I came up with could be to create DateTime values for an entire week, and then use date.ToString("dddd", new CultureInfo("it-IT")); on them, but that just seems wrong on so many levels.


Solution

  • How about

    DateTimeFormatInfo.CurrentInfo.GetDayName( dayOfWeek )
    

    or

    DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName( dayOfWeek )
    

    Simply takes a DayOfWeek enumeration and returns string that is the name in the current culture.

    Edit:

    If you're looking for a specific culture it would be

    var cultureInfo = new CultureInfo( "it-IT" );
    var dateTimeInfo = cultureInfo.DateTimeFormat;
    

    then you can call GetDayName or GetAbbreviatedDayName as you choose.

    There's even a AbbreviatedDayNames/DayNames arrays if you want them all