编程语言
首页 > 编程语言> > C#-按工作的第一天对工作日进行排序

C#-按工作的第一天对工作日进行排序

作者:互联网

有没有更好的方法来重新安排

DateTimeFormat.DayNames

根据

DateTimeFormat.FirstDayOfWeek

这样,如果

DateTimeFormat.FirstDayOfWeek = 1

dayNames包含周一,周二,周三,…
受儿子,星期一,星期二等…
我目前正在使用:

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)];
}

解决方法:

您已经有效地实现了模运算符,因此可以将表达式简化为

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

标签:globalization,c
来源: https://codeday.me/bug/20191107/2003029.html