C# .NET - To Calculate number of days with date selected

Asked By abinav shankar on 07-Feb-12 07:35 AM


I have two calendar controls one for From and the other for To and from the two dates selected i have to calculate the number of days and also i have to exclude the saturdays and sundays (i.e) if i have applied for leave from friday till monday I have to exclude sat and sunday and calculate the leave as 2 how to do it

dipa ahuja replied to abinav shankar on 07-Feb-12 07:36 AM
 void countDays()
  DateTime d1 = DateTime.Parse(txtFrom.Text);
  DateTime d2 = DateTime.Parse(txtTo.Text);
  TimeSpan span = d2 - d1;
     ("There're {0} days between {1} and {2}", span.TotalDays, d1.ToString(), d2.ToString());
Amit Choudhary replied to abinav shankar on 07-Feb-12 08:23 AM
I m afraid that you have to run a loop in c# for it.
In while loop check ..
        DateTime d1, d2;
        int count = 0;
        while (d1 < d2)
          if (d1.DayOfWeek != DayOfWeek.Saturday && d1.DayOfWeek != DayOfWeek.Sunday)
            d1 = d1.AddDays(1);
Danasegarane Arunachalam replied to abinav shankar on 07-Feb-12 08:33 AM
Use the below function to get the workings days between two dates

public static int GetWorkingDay(DateTime startdayday, DateTime enddayday)
	if (startday.DayOfWeek == DayOfWeek.Saturday) {
		startday = startday.AddDays(2);
	} else if (startday.DayOfWeek == DayOfWeek.Sunday) {
		startday = startday.AddDays(1);

	if (endday.DayOfWeek == DayOfWeek.Saturday) {
		endday = endday.AddDays(-1);
	} else if (endday.DayOfWeek == DayOfWeek.Sunday) {
		endday = endday.AddDays(-2);

	int diff = Convert.ToInt32(endday.Subtract(startday).TotalDays);

	int result = (diff / 7) * 5 + diff % 7;

	if (endday.DayOfWeek < startday.DayOfWeek) {
		return result - 2;
	} else {
		return result;

And call the function as

int workingdays=GetWorkingDay(Calendar1.Value,Calendar2.Value)
[)ia6l0 iii replied to abinav shankar on 07-Feb-12 09:04 AM
I would suggest you to have a extension method on the datetime object, so that you can exclude holidays at a later stage. We always start with weekends , and then you would ask for holidays. So the extension method would be a simpler way of placing this logic.

First extension method to check if it is a weekday.
public static bool WeekDay(this DateTime date)
        return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday;

Second extension method to retrieve all the dates in the range. So that we can compare the dates if they fall on a weekday. 
public static IEnumerable<DateTime> GetDatesInTheRange(this DateTime startdate, this DateTime enddate)
            DateTime pointerDate = start;
            while (pointerDate < end)
                yield return pointerDate;
                pointerDate = pointerDate.AddDays(1);

And then simply use with the following code:
List<DateTime> allDates = GetDatesInTheRange(calendar1date - calendar2date);
int leavesApplied = allDates.Count(day => day.WeekDay()).

Hope this helps.