C# .NET - How to calculate num of weekdays between 2 dates

Asked By jez enefer-mills on 14-Nov-05 05:34 AM
```Hi everyone,

Sorrry to trouble you all again.

I have 2 dates which I subtract from each other to find out the number of days between them.

As follows: -

this.txttotdays.Text = (DateTime.Parse(this.txtDate4.Text) - DateTime.Parse(this.txtDate1.Text)).Days.ToString();

Can someone show me how to modify this line of code to get a count of weekdays only

Thanks```

try - Asked By Sathish S on 14-Nov-05 06:31 AM

```DateTime  asof,bday;
TimeSpan span = asof - bday;
Console.Writeline(span.Days());```

Weekdays only - Asked By jez enefer-mills on 14-Nov-05 06:47 AM

```I used you suggestion is this code: -

TimeSpan span = (DateTime.Parse(this.txtDate4.Text) - DateTime.Parse(this.txtDate1.Text));
this.txtTotDays2.Text = span.Days.ToString();

but got an answer including weekends

Can you help further```

Something like this - Asked By Aarthi Saravanakumar on 14-Nov-05 06:50 AM

```You need to itereate over the DateTimes
and find the DayOfWeek of every dateTime. If this is Monday through Friday then Increment your  counter by1.

http://www.dotnet247.com/247reference/msgs/32/162879.aspx```
Here's a formula based approach - Asked By Bernal Schooley on 14-Nov-05 01:38 PM
```I saw examples using a loop and counter, but it could cause slow performance in some circumstances, so I set about finding a formula based approach. I was unable to find such a formula after digging deep into Google, so I wrote my own. Let me just say it was a lot more challenging than I thought it would be.

int Weekdays(DateTime dtmStart, DateTime dtmEnd)
{
// This function includes the start and end date in the count if they fall on a weekday
int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);
int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);
TimeSpan tSpan = dtmEnd - dtmStart;
if (dowStart <= dowEnd)
{
return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));
}
else
{
return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));
}
}```
Bulldog replied to Bernal Schooley on 29-Jul-10 04:09 PM
I found this a little har to follow, so i made this one.

public static int Weekdays(DateTime startDate, DateTime endDate)

{

if (startDate > endDate)

{

Swap<DateTime>(ref startDate, ref endDate);

}

int weekDaysInFirstWeek = Math.Min(DayOfWeek.Saturday - startDate.DayOfWeek, 5);

int weekDaysInLastWeek = Math.Min((int)endDate.DayOfWeek, 5);

//set startdate on the saterday of the week

//set enddate on the sunday of the week

//number of full weeks between the dates

int fullWeeksbetween = (endDate - startDate).Days / 7;

return (fullWeeksbetween * 5) + weekDaysInFirstWeek + weekDaysInLastWeek;

}

Bulldog replied to Bulldog on 29-Jul-10 04:33 PM

Oops small mistake there
public static int Weekdays(DateTime startDate, DateTime endDate)

{

if (startDate > endDate)

{

Swap<DateTime>(ref startDate, ref endDate);

}

int weekDaysInFirstWeek = Math.Min(DayOfWeek.Saturday - startDate.DayOfWeek, 5);

int weekDaysInLastWeek = Math.Min((int)endDate.DayOfWeek, 5);

//set startdate on the next sunday

startDate = startDate.AddDays(DayOfWeek.Saturday - startDate.DayOfWeek + 1).Date;

//set enddate on the saterday of the week