计数正常工作日内在给定的时间段 [英] Counting regular working days in a given period of time
问题描述
需要一些帮助。我需要计数正常工作日内给定日期时间,例如,在我们的国家,我们有5个普通的工作日周一至周五,然后在codeI需要排除星期六和星期天,当我用我的计算。
我需要在C#中的算法是这样的:
INT GetRegularWorkingDays(日期时间的startDate,日期时间结束日期)
{
INT nonWorkingDays =((结束日期 - 的startDate)%7)* 2;
返回(结束日期 - 的startDate) - nonWorkingDays;
}
我知道我的草案的方式路要走:(。在此先感谢。=)
PS:大家好请了票的最佳/最快/最有效的下面的答案。谢谢=)
查看这个例子在code ++项目使用,不涉及任何循环一个非常有效的方式;)
它使用该alogrithm:
- 计算在周计的时间跨度的数目。调用它,W。
- 从周数扣除的第一周。 W = W-1
- 乘周数与工作日每数 周。说它,D。
- 找到了指定的时间跨度在节假日期间。调用它,H。
- 计算天中的第一周。调用它,SD。
- 计算天在上周。调用它,ED。
- 在总结的日子。 BD = D + SD + ED小时。
need some help. I need to count regular working days for a given date period, for example, in our country, we have 5 regular working days monday to friday, then in code i need to exclude saturdays and sundays when I use it on my computations.
I need an algorithm something like this in C#:
int GetRegularWorkingDays(DateTime startDate, DateTime endDate)
{
int nonWorkingDays = ((endDate - startDate) % 7) * 2;
return (endDate - startDate) - nonWorkingDays;
}
I know my draft is way way off :(. Thanks in advance. =)
PS: Guys please up-vote the best/fastest/most efficient answer below. Thanks =)
Check out this example on Code Project that uses a very efficient way that doesn't involve any looping ;)
It uses this alogrithm:
- Calculate the number of time span in terms of weeks. Call it, W.
- Deduct the first week from the number of weeks. W= W-1
- Multiply the number of weeks with the number of working days per week. Call it, D.
- Find out the holidays during the specified time span. Call it, H.
- Calculate the days in the first week. Call it, SD.
- Calculate the days in the last week. Call it, ED.
- Sum up all the days. BD = D + SD + ED � H.
这篇关于计数正常工作日内在给定的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!