In C#, how can I calculate the number of business (or weekdays) days between two dates?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Here's yet another idea - this method allows to specify any working week and holidays.
The idea here is that we find the core of the date range from the first first working day of the week to the last weekend day of the week. This enables us to calculate the whole weeks easily (without iterating over all of the dates). All we need to do then is to add the working days that fall before the start and end of this core range.
Define an Extension Method on DateTime like so:
Then, use is within a Where clause to filter a broader list of dates:
Ok. I think it's time to post the right answer:
Original Source:
http://alecpojidaev.wordpress.com/2009/10/29/work-days-calculation-with-c/
P.S. Solutions posted above making me sic for some reason.
Since I can't comment. There is one more issue with the accepted solution where bank holidays are subtracted even when they are situated in the weekend. Seeing how other input is checked, it is only fitting that this is as well.
The foreach should therefore be:
Based on the comment marked as answer and patch recommended , as well as -> This version wants to convert the Days to Business-Hours ... Considers Same day hours as well.