按工作第一天对工作日进行排序

发布于 2024-08-03 09:32:24 字数 1000 浏览 7 评论 0原文

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

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

is there a better way of rearange the

DateTimeFormat.DayNames

acording to the

DateTimeFormat.FirstDayOfWeek

so that if

DateTimeFormat.FirstDayOfWeek = 1

dayNames containts mon,tue,wed, ...
insted of son,mon,tue, ...
I'm currently using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-08-10 09:32:24

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

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

You've effectively implemented to modulo operator, so you can simplify your expression to

tempdays[i] = DayNames[ (FirstDayOfWeek + i) % 7 ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文