Sort days of the week on the first day of work

there is a better way to regroup

DateTimeFormat.DayNames

in accordance with

DateTimeFormat.FirstDayOfWeek

so if

DateTimeFormat.FirstDayOfWeek = 1

dayNames contains mon, tue, wed, ...
son’s instinct, mon, t, ...
Currently I use:

CultureInfo culture = System.Globalization.CultureInfo.CurrentUICulture;
string[] DayNames = culture.DateTimeFormat.DayNames;
int FirstDayOfWeek = (int)culture.DateTimeFormat.FirstDayOfWeek;
int daysInMonth = DateTime.DaysInMonth(year, month);
string[] tempdays = new string[7];
for (int i = 0; i <= DayNames.GetUpperBound(0); i++)
{
    tempdays[i] = DayNames[i == 0 ? FirstDayOfWeek : 
                           (i == 6 - FirstDayOfWeek ? 6 : 
                           i > 6 - FirstDayOfWeek ? 0 != (6 - (i + (FirstDayOfWeek - 1))) ? 
                           (6 - (i + (FirstDayOfWeek - 1))) * -1 : 
                           6 - (i + (FirstDayOfWeek - 1)) : i + FirstDayOfWeek)];
}
+3
source share
1 answer

The modulo operator is effectively implemented, so you can simplify your expression to

tempdays[i] = DayNames[ (FirstDayOfWeek + i) % 7 ];
+4
source

Source: https://habr.com/ru/post/1716300/


All Articles