Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to get the number of days between two dates, excluding weekends?

Posted on 2006-05-18
7
Medium Priority
?
5,473 Views
Last Modified: 2007-12-19
Hello all,

I've searched and a found a couple of suggestions that didn't work for me.

I can get the amount of days with the weekends included in the count. I can't seem to figure out how to get the number of days without the weekends.

DateTime dt1 = Convert.ToDateTime(FirstDayOut.Text);
DateTime dt2 = Convert.ToDateTime(WillReturn.Text);
           
TimeSpan ts = dt2.Subtract(dt1);
int days = ts.Days;
txtDate.Text = days.ToString();

This is very urgent.
Any help would be great.

-Jordan
0
Comment
Question by:JordanBlackler
7 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 16711498
I haven't worked out the exact code, but you can do something like this:

System.DayOfTheWeek day1 = dt1.DayOfTheWeek;
System.DayOfTheWeek day2 = dt2.DayOfTheWeek;


then you need to figure out from the number of days, and the day or the week you start and stop how many weekend days are between there.
0
 
LVL 8

Expert Comment

by:gena17
ID: 16711804
May be this one will work for you:

            TimeSpan ts = dt2 - dt1;
            int totalDays = (int)ts.TotalDays;
            int fullWeeks = totalDays / 7;
            int daysRest = totalDays - fullWeeks * 7;
            totalDays -= fullWeeks * 2;

            DateTime day = dt2.AddDays(-daysRest);
            for (int i=0; i<daysRest; i++)
            {
                if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
                {
                    totalDays--;
                }
                day = day.AddDays(1);
            }

            Console.WriteLine(totalDays);

Just be careful with times of your dates.

Gena
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 2000 total points
ID: 16713893
Maybe this would help you out.

I just converted on the fly from COld Fusion (there I also have holidays in a text file which I can add to this if you need to account for that as well...

int BusinessDateDiff(DateTime startDate, DateTime endDate)
{

// Local variables for date computations
int start_day = startDate.DayOfWeek;
int end_day = endDate.DayOfWeek;
int date_diff_in_days = (((TimeSpan)(endDate - startDate)).Days;
int whole_weeks = date_diff_in_days\7;
int days_left = date_diff_in_days %7;

// Subtract weekends in whole weeks
int non_bus_days = whole_weeks * 2;

// Subtract partial or whole weekends for partial weeks
if(days_left)
{
   if(end_day == 7 || start_day == 7)
       non_bus_days++;
   else if (startDate.Date > endDate.Date)
      non_bus_days = non_bus_days + 2;
}

// total business days
return date_diff_in_days - non_bus_days;

}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:mrichmon
ID: 16713895
SInce I just converted on the fly as I typed you may need to touch up a bit, but the algorithm is sound.
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 16713908
For example this line:
if(end_day == 7 || start_day == 7)

WOuld be better as:

if(end_day == DayOfWeek.Saturday || start_day == DayOfWeek.Saturday)
0
 

Expert Comment

by:joinsaad
ID: 27614906
I have a better and simple code
here it goes

public static  int BusinessDateDiff(DateTime startDate, DateTime endDate)
    {
           
            int dayscount = 0;

            DateTime BDate = startDate;

            while (BDate <= endDate)
            {
                if(BDate.DayOfWeek != DayOfWeek.Sunday)
                dayscount++;
                BDate = BDate.AddDays(1);
            }

            return dayscount;
}
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 27620779
nice try, but it is not "better and simpler" as it gives the wrong answer, doesn't account for holidays, and the whole point was to calculate NOT loop.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Integration Management Part 2
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

575 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