tags:

views:

176

answers:

4

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"
+2  A: 

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;
    }
}
Dean Harding
Agreed, I think this is probably easiest. I ended up using `if (CurrentCulture.TwoLetterISOLanguageName == "en")` to check for English.
cxfx
+2  A: 

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.

Josh Einstein
Wow, I never knew it was that easy!
Dean Harding
Thanks for your answer, I already tried this and was disappointed to still get the days back in the same order. Good idea though.
cxfx
+3  A: 

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...

Josh Einstein
Nice solution, I like the Shift method, a handy one to keep in the library.
cxfx
+1  A: 

One more idea, inspired by Josh's answer, using a Queue instead of shifting the array.

var days = CultureInfo.CurrentCulture.DateTimeFormat.DayNames;
if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName == "en")
{
    var q = new Queue<string>(days);
    q.Enqueue(q.Dequeue());
    days = q.ToArray();
}
cxfx
Clever! It sure is easier on the eyes than the direct array manipulation.
Josh Einstein