计数正常工作日内在给定的时间段 [英] Counting regular working days in a given period of time

查看:132
本文介绍了计数正常工作日内在给定的时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助。我需要计数正常工作日内给定日期时间,例如,在我们的国家,我们有5个普通的工作日周一至周五,然后在codeI需要排除星期六和星期天,当我用我的计算。

我需要在C#中的算法是这样的:

  INT GetRegularWorkingDays(日期时间的startDate,日期时间结束日期)
    {

       INT nonWorkingDays =((结束日期 - 的startDate)%7)* 2;

       返回(结束日期 - 的startDate) -  nonWorkingDays;
    }
 

我知道我的草案的方式路要走:(。在此先感谢。=)

PS:大家好请了票的最佳/最快/最有效的下面的答案。谢谢=)

解决方案

查看这个例子在code ++项目使用,不涉及任何循环一个非常有效的方式;)

它使用该alogrithm:

  
      
  1. 计算在周计的时间跨度的数目。调用它,W。
  2.   
  3. 从周数扣除的第一周。 W = W-1
  4.   
  5. 乘周数与工作日每数   周。说它,D。
  6.   
  7. 找到了指定的时间跨度在节假日期间。调用它,H。
  8.   
  9. 计算天中的第一周。调用它,SD。
  10.   
  11. 计算天在上周。调用它,ED。
  12.   
  13. 在总结的日子。 BD = D + SD + ED小时。
  14.   

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:

  1. Calculate the number of time span in terms of weeks. Call it, W.
  2. Deduct the first week from the number of weeks. W= W-1
  3. Multiply the number of weeks with the number of working days per week. Call it, D.
  4. Find out the holidays during the specified time span. Call it, H.
  5. Calculate the days in the first week. Call it, SD.
  6. Calculate the days in the last week. Call it, ED.
  7. Sum up all the days. BD = D + SD + ED � H.

这篇关于计数正常工作日内在给定的时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆