How to Display Day Names In The Right Order
Posted in Development
What happens when you call AbbreviatedDayNames is you get an array of day names in the language of the current culture. There’s a caveat: the array always starts with a Sunday even if the first day in that particular culture is Monday. This disconnect can be very confusing if you’re plotting day names against a chart axis, for example.
To list day names in the right order, you have to rely on the FirstDayOfWeek property. Basically, you need to rearrange day names based on what the first day of the week actually is.
Below is a snippet that does exactly that (derived from the
Calendar control source):
DateTimeFormatInfo dtfi = CultureInfo.CurrentUICulture.DateTimeFormat; List<string> dayNames = new List<string>(7); int firstDayIndex = (int) dtfi.FirstDayOfWeek; for (int i = firstDayIndex; i < (firstDayIndex + 7); i++) { int adjustedIndex = i % 7; string dayName = dtfi.GetAbbreviatedDayName ((DayOfWeek) adjustedIndex); dayNames.Add (dayName); }
Below are results for the en-US (English / United States) and de-AT (Deutsch/Österreich) cultures respectively:
[ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ] [ "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" ]
