Solved

C# number of workdays

Posted on 2009-05-12
8
1,088 Views
Last Modified: 2012-05-06
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
Comment
Question by:SimpleJ
  • 5
  • 2
8 Comments
 
LVL 3

Assisted Solution

by:crystal_tower
crystal_tower earned 100 total points
Comment Utility
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

by:abel
Comment Utility
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

by:
abel earned 400 total points
Comment Utility

try again that link: http://Q_24377674.html

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) {

        dateFrom = dateFrom.AddDays(1);

        DayOfWeek weekDay = dateFrom.DayOfWeek;

        if (weekDay != DayOfWeek.Saturday & weekDay != DayOfWeek.Sunday)  

        {

             countDays += 1;

        }

    }

    return countDays;

}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:SimpleJ
Comment Utility
Thank you
0
 
LVL 39

Expert Comment

by:abel
Comment Utility
> 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

by:abel
Comment Utility
objection reviewers: see comment http:#24363631
0
 
LVL 1

Author Comment

by:SimpleJ
Comment Utility
Thanks for the info.

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now