Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# C# number of workdays

Posted on 2009-05-12
Medium Priority
1,098 Views
Hello.

I would like to get number of working days in current month and year until today (C#).

e.g.: today is 12.5.2009 -->  8 working days
tomorrow it will be 13.5.2009 --> 9 working days

Thank you.
0
Question by:SimpleJ
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 2

LVL 3

Assisted Solution

crystal_tower earned 400 total points
ID: 24363026
You need to count days without free ones, basic version:
- count all days without weekends, like:
DateTime theDateFrom;
DateTime theDateTo;
...
if(theDateFrom.DayOfWeek != DayOfWeek.Saturday && theDateFrom.DayOfWeek != DayOfWeek.Sunday)
workdays++;
count till theDateFrom <= theDateTo

- however remember about bank holidays. Look for instance:
http://www.codeproject.com/KB/dotnet/HolidayCalculator.aspx

- finally, all depends on calendar. Saturday or Sunday could be a working day.

0

LVL 39

Expert Comment

ID: 24363044
In http:Q_24377674.html I explained this process for VB, but that's the other way around (given the number of business days, how many "normal" days would that be?).
0

LVL 39

Accepted Solution

abel earned 1600 total points
ID: 24363089

anyway, crystal_tower gave the same idea. Here's how I did it (full loop):
``````private int GetBusinessDays(DateTime dateFrom, DateTime dateTo)
{
Â  Â  int countDays = 0;
Â  Â
Â  Â  while (dateFrom < dateTo) {
Â  Â  Â  Â  DayOfWeek weekDay = dateFrom.DayOfWeek;
Â  Â  Â  Â  if (weekDay != DayOfWeek.Saturday & weekDay != DayOfWeek.Sunday)
{
countDays += 1;
Â  Â  Â  Â  }
Â  Â  }
Â  Â  return countDays;
}
``````
0

LVL 39

Expert Comment

ID: 24363095
something wrong with my pasting of links at EE... Sorry about the clutter. Last time: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_24377674.html
0

LVL 1

Author Comment

ID: 24363544
Thank you
0

LVL 39

Expert Comment

ID: 24363631
> I used abel s method.

nice and tx. You accidentally hit the "Accept and Award Point" of your own comment (which effectively makes your comment the solution, which it isn't), instead, I believe you wanted to assign points to me and crystal_tower. You can do so by clicking the "Accept Multiple Solutions". See screenshot for the difference between the two.

Note that the first one (the one in red) is only considered good if you found the solution yourself. Also see closing questions.

ScreenShot270.png
0

LVL 39

Expert Comment

ID: 24363646
objection reviewers: see comment http:#24363631
0

LVL 1

Author Comment

ID: 24363717
Thanks for the info.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue canâ€¦
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month6 days, 18 hours left to enroll