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"
This should also work nice.
DatetimeFormatInfo.GetDayName obviously, with a foreach on each DayOfWeek in the order you want. :) No hack, simple and effective
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.)
I meant to post this sooner but I am fighting a dying external hard drive...
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:
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:
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:
You can Clone the current culture which gets a writable copy of the CultureInfo object. Then you can set the DateTimeFormat.FirstDayOfWeek to 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.