38

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.

bevacqua
  • 47,502
  • 56
  • 171
  • 285

2 Answers2

90

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

MerickOWA
  • 7,453
  • 1
  • 35
  • 56
  • Can you clarify please. If I serialised a DayOfWeek e I’m to XML and the user changes the pc locale , will this affect how the day is saved / read? Eg if it has Monday in xml and now pc is Spanish will it still know that Monday is Lunes? Does the xml internally always serialise with English days of week? I am not in a position to test but my app is in 50+ languages so it is an area of concern. – Andrew Truckle Sep 03 '23 at 05:39
0

In addition to MerickOWA's answer, we can now create extension methods:

public static class DayOfWeekExtensions
{
    public static string GetDayName(this DayOfWeek value)
    {
        return DateTimeFormatInfo.CurrentInfo.GetDayName(value);
    }

    public static string GetAbbreviatedDayName(this DayOfWeek value)
    {
        return DateTimeFormatInfo.CurrentInfo.GetAbbreviatedDayName(value);
    }
}
Michael Csikos
  • 688
  • 9
  • 11