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

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

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

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. Below link elaborates this http://www.dotnet247.com/247reference/msgs/32/162879.aspx

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

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

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

//set enddate on the sunday of the week

endDate = endDate.AddDays(-1 * (int)endDate.DayOfWeek).Date;

//number of full weeks between the dates

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

return (fullWeeksbetween * 5) + weekDaysInFirstWeek + weekDaysInLastWeek;

}

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

endDate = endDate.AddDays(-1 * (int)endDate.DayOfWeek).Date;

//number of full weeks between the dates

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

return (fullWeeksbetween * 5) + weekDaysInFirstWeek + weekDaysInLastWeek;

}