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-开发者_C百科IT"));
on them, but that just seems wrong on so many levels.
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
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);
}
}
精彩评论