gpt4 book ai didi

c# - 计算一周内工作的天数而不循环?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:17 25 4
gpt4 key购买 nike

我需要计算工作的总天数,给定结构说明一周中的哪几天工作以及从和到日期。

我目前的算法是这样的:

protected int TotalWorkDays(DateTime From, DateTime dtTo,WorkedDays days)
{
int Days = 0;
DayOfWeek DOW;
for (DateTime curDate = From; curDate.Date <= dtTo.Date; curDate = curDate.AddDays(1))
{
DOW = curDate.DayOfWeek;
if ((DOW == DayOfWeek.Sunday & days.Sunday) |
(DOW == DayOfWeek.Monday & days.Monday) |
(DOW == DayOfWeek.Tuesday & days.Tuesday) |
(DOW == DayOfWeek.Wednesday & days.Wednesday) |
(DOW == DayOfWeek.Thursday & days.Thursday) |
(DOW == DayOfWeek.Friday & days.Friday) |
(DOW == DayOfWeek.Saturday & days.Saturday)
)
{
Days += 1;
}
}
return Days;
}

我几乎肯定这可以在没有循环的情况下完成,但我似乎无法弄清楚。谁能帮我找到更高效的算法?

最佳答案

计算从和到日期之间的周数(使用减法和除法)。然后将其乘以每周工作的天数。对最终案例做一些减法(从/到日期在一周的中间)。

关于c# - 计算一周内工作的天数而不循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6374706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com