Getting current culture day names in .NET

2020-03-12 05:02发布

Is it possible to get the CurrentCulture's weekdays from DateTimeFormatInfo, but returning Monday as first day of the week instead of Sunday. And, if the current culture isn't English (i.e. the ISO code isn't "en") then leave it as default.

By default CultureInfo.CurrentCulture.DateTimeFormat.DayNames returns:

[0]: "Sunday"
[1]: "Monday"
[2]: "Tuesday"
[3]: "Wednesday"
[4]: "Thursday"
[5]: "Friday"
[6]: "Saturday" 

But I need:

[0]: "Monday"
[1]: "Tuesday"
[2]: "Wednesday"
[3]: "Thursday"
[4]: "Friday"
[5]: "Saturday" 
[6]: "Sunday"

标签: c# .net datetime
8条回答
叛逆
2楼-- · 2020-03-12 05:10

This should also work nice.

    public static List<String> Days
    {
        var abbDayNames = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedDayNames;

        var days = new string[7];
        var firstDayOfWeek = (int)DayOfWeek.Monday;
        for (int i = 6; i>= 0; i--)
        {
            days[i] = abbDayNames[(firstDayOfWeek + i) % 7];
        }

        return new List<string>(days);
    }
查看更多
我想做一个坏孩纸
3楼-- · 2020-03-12 05:13

DatetimeFormatInfo.GetDayName obviously, with a foreach on each DayOfWeek in the order you want. :) No hack, simple and effective

查看更多
趁早两清
4楼-- · 2020-03-12 05:21

I am posting this as a separate answer as it really has nothing to do with my other answer (which may be useful to someone else in the future in another context.)

As an alternative to codeka's solution, you can also do something like this (which would avoid having to hard code the en-us day names.)

string[] dayNamesNormal = culture.DateTimeFormat.DayNames;
string[] dayNamesShifted = Shift(dayNamesNormal, (int)DayOfWeek.Monday);

// you probably wanna add some error checking here.
// this method shifts array left by a specified number
// of positions, wrapping the shifted elements back to
// end of the array
private static T[] Shift<T>(T[] array, int positions) {
    T[] copy = new T[array.Length];
    Array.Copy(array, 0, copy, array.Length-positions, positions);
    Array.Copy(array, positions, copy, 0, array.Length-positions);
    return copy;
}

I meant to post this sooner but I am fighting a dying external hard drive...

查看更多
仙女界的扛把子
5楼-- · 2020-03-12 05:23

You can use custom cultures to create a new culture based off an existing one. To be honest, though, I'd say that's probably a bit heavy-handed. The "simplest" solution may just be something like:

public string[] GetDayNames()
{
    if (CultureInfo.CurrentCulture.Name.StartsWith("en-"))
    {
        return new [] { "Monday", "Tuesday", "Wednesday", "Thursday",
                        "Friday", "Saturday", "Sunday" };
    }
    else
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
    }
}
查看更多
霸刀☆藐视天下
6楼-- · 2020-03-12 05:24

If you are getting day names based on dates, it doesn't matter what day the week starts on; DateTimeFormat.DayNames identifies Sunday as 0, as does DateTime, no matter if weeks start on Thursday or what have you. :)

To get day name in English from a date:

string GetDayName(DateTime dt)
{
    return CultureInfo.InvariantCulture.DateTimeFormat.DayNames[(int)dt.DayOfWeek];
}

If for some reason you absolutely want to deal with the (magic value!) ints that underpin the DayOfWeek enumeration, just shift the index and take the modulus, hence mapping 0 => 6, 1 => 0, and so on:

string GetDayName(int dayIndex)
{
    return CultureInfo.InvariantCulture.DateTimeFormat.DayNames[(dayIndex + 6) % 7]; 
}
查看更多
ら.Afraid
7楼-- · 2020-03-12 05:25

You can Clone the current culture which gets a writable copy of the CultureInfo object. Then you can set the DateTimeFormat.FirstDayOfWeek to Monday.

CultureInfo current = CultureInfo.Current;
CultureInfo clone = (CultureInfo)current.Clone();

clone.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;

The above clone will now treat Monday as the first day of the week.

EDIT

After re-reading your question I don't think this will do what you're expecting. The DayNames will still return in the same order regardless of the FirstDayOfWeek setting.

But I'll leave this answer up as community wiki in case someone comes across this question in the future.

查看更多
登录 后发表回答