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
Medium Priority
5,473 Views
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
Question by:JordanBlackler

LVL 28

Expert Comment

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

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;

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

Console.WriteLine(totalDays);

Just be careful with times of your dates.

Gena
0

LVL 35

Accepted Solution

mrichmon earned 2000 total points
ID: 16713893

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...

{

// 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;
}

return date_diff_in_days - non_bus_days;

}
0

LVL 35

Expert Comment

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

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

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++;
}

return dayscount;
}
0

LVL 35

Expert Comment

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

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