C# number of workdays

Posted on 2009-05-12
Last Modified: 2012-05-06

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.
Question by:SimpleJ
  • 5
  • 2

Assisted Solution

crystal_tower earned 100 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)
count till theDateFrom <= theDateTo

- however remember about bank holidays. Look for instance:

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

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?).
LVL 39

Accepted Solution

abel earned 400 total points
ID: 24363089

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

LVL 39

Expert Comment

ID: 24363095
something wrong with my pasting of links at EE... Sorry about the clutter. Last time:

Author Comment

ID: 24363544
Thank you
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.

LVL 39

Expert Comment

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

Author Comment

ID: 24363717
Thanks for the info.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing 1 60
Problem!!! 8 37
tableview is not updating 1 22
Using data from certificate to re-create certificate programmatically 3 27
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

820 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