Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current culture day names in .NET

Tags:

c#

.net

datetime

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"
like image 523
Chris Fulstow Avatar asked May 25 '10 00:05

Chris Fulstow


1 Answers

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.

like image 53
2 revs Avatar answered Sep 23 '22 09:09

2 revs