I think none of the above answers are actually correct. None of them solves all the special cases such as when the dates starts and ends on the middle of a weekend, when the date starts on a Friday and ends on next Monday, etc. On top of that, they all round the calculations to whole days, so if the start date is in the middle of a saturday for example, it will substract a whole day from the working days, giving wrong results Anyway, here is my solution that is quite efficient and simple and works for all cases.
The trick is just to find the previous Monday for start and end dates, and then do a small compensation when start and end happens during the weekend:. Here is the function which we can use to calculate business days between two date. If we want to use it anyway we can take third argument as list of holiday and before incrementing count we should check that list does not contains d. I'll just share my solution. I started by getting the first incomplete week if there's any. DayOfWeek was not 0 Sunday , the first week was incomplete.
I was having trouble finding a solid TSQL version of this code. Below is essentially a conversion of the C code here with addition of the Holiday table which should be used to pre-calculate holidays. Here is one very simple solution for this problem. We have starting date, end date and "for loop" for encreasing the day and calculating to see if it's a workday or a weekend by converting to string DayOfWeek.
Considers Same day hours as well. I just improved Alexander and Slauma answer to support a business week as a parameter, for cases where saturday is a business day, or even cases where there is just a couple of days of the week that are considered business days:.
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.
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. Here is an approach if you are using MVC. I have also calculated national holidays or any festive days to be excluded by fetching it from holidayscalendar which you will need to make one.
Here is an helper function I wrote for that task. You just have to iterate through each day in the time range and subtract a day from the counter if its a Saturday or a Sunday. I'm only posting this because despite all of the excellent answers that have been given, none of the math made sense to me. This is definitely a KISS method that should work and be fairly maintainable. Granted if you are calculating ranges that are greater than months this will not be the most effective way.
We simply determine if it is a Saturday or Sunday or the date is a given holiday date. If it's not we add a business day. If it is then everything is fine. I'm sure this could be even more so simplified with LINQ, but this way is much easier to understand. Yet another approach for calculating business days, not considering holidays, but taking into account the time of day returning a fractional amount of days:.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Calculate the number of business days between two dates? Ask Question. Asked 12 years ago. Active 1 month ago. Viewed k times. Improve this question.
Partha Sarathi Ghosh 9, 18 18 gold badges 55 55 silver badges 80 80 bronze badges. Martin Martin Here is an example in C that also extends into getting hours as well. Take a look at infopathdev.
Add a comment. Active Oldest Votes. Improve this answer. Slauma k 57 57 gold badges silver badges bronze badges. Alexander Alexander 1, 1 1 gold badge 9 9 silver badges 4 4 bronze badges.
IsWorkingDay — Tawani. Thanks for the method. Sunday bh. Saturday , else it would substract the same day twice, if a holiday falls in a weekend. The first day of the week can change according to user settings. Also they are countries that don't have the weekend in Saturday, Sunday. See this link for more info: en. Show 5 more comments. DayOfWeek - endD. Saturday calcBusinessDays--; if startD. Alec Pojidaev Alec Pojidaev 1, 1 1 gold badge 7 7 silver badges 3 3 bronze badges.
Good work, but perhaps use the DayOfWeek enums themselves rather than cast them to ints? Seriously, best solution out there. Cheers Alec — Mizmor. Note that even though this function returns a double, it should only be trusted to give whole business days.
It does not return the correct answer for fractional days when times are involved. Took me a while to figure that out, since I was assuming start of first day until start of last day, which made more sense to me. This is NOT the correct answer. Days can be out by upto 4. Almost right, doesn't take account of when the start and end day wrap around the weekend, which is the trickiest bit.
The start - end shouldn't be inside the parentheses either. It's got nothing to do with the problem. Show 10 more comments. Subtract from. TotalDays; return Enumerable. Range 1, dayDifference. AddDays x. Camilo Terevinto Alpha Alpha 7, 8 8 gold badges 59 59 silver badges 89 89 bronze badges.
Sign up. By signing in, you agree to our Terms of Use and Privacy Policy. Already have an account? Sign in. By signing up, you agree to our Terms of Use and Privacy Policy. Enter the email address associated with your account. We'll send a magic link to your inbox. Email Address. All Sign in options. Enter a Email Address. Choose your interests Get the latest news, expert insights and market research, sent straight to your inbox.
Newsletter Topics Select minimum 1 topic. Joanna17 February 15, 0 Comments. Anonymous Posted February 16, 0 Comments. Register or Login. Big Data. Welcome back! How many weekends are there in 50 years? How many Sundays are there in a year? Can there be 53 Sundays in a year? Is a leap year? How many weeks are in a year in ? How many days are in a month without Sunday? How many Saturdays and Sundays fall into a calendar year?
How many days are in a year without weekends? How to count the number of weekend days between two dates? How many days are there in the year ? How many total days and weekdays are there between two dates? How do you calculate days without Saturday and Sunday? How many days were there in ? How many days are in a month without Sundays?
How many weeks is a 14 year old? What day of the week was Christmas on in ? How many working days are there in ? How many hours is 50 years?
How many Sundays are in a year in ? How many days are there in a month without weekends?
0コメント