Is it possible to get the CurrentCulture
\'s weekdays from DateTimeFormatInfo
, but returning Monday as first day of the week instea
Since only one day is being moved, I solved this problem like this:
'the list is from Sunday to Saturday
'we need Monday to Sunday
'so add a Sunday on the end and then remove the first position
dayNames.AddRange(DateTimeFormatInfo.CurrentInfo.DayNames)
dayNames.Add(DateTimeFormatInfo.CurrentInfo.GetDayName(DayOfWeek.Sunday))
dayNames.RemoveAt(0)
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;
}
}
DatetimeFormatInfo.GetDayName obviously, with a foreach on each DayOfWeek in the order you want. :) No hack, simple and effective
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();
}
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.
Another simple solution:
var dayNames = Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames.ToList();
if (Culture.DateTimeFormat.FirstDayOfWeek == DayOfWeek.Monday)
{
dayNames.Add(dayNames[0]);
dayNames.RemoveAt(0);
}