Hi

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

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)

{

count++;

d1 = d1.AddDays(1);

}

}

In while loop check ..

DateTime d1, d2;

int count = 0;

while (d1 < d2)

{

if (d1.DayOfWeek != DayOfWeek.Saturday && d1.DayOfWeek != DayOfWeek.Sunday)

{

count++;

d1 = d1.AddDays(1);

}

}

Use the below function to get the workings days between two dates

And call the function as

int workingdays=GetWorkingDay(Calendar1.Value,Calendar2.Value)

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)

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.

First extension method to check if it is a weekday.

{

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.

{

DateTime pointerDate = start;

while (pointerDate < end)

{

yield return pointerDate;

pointerDate = pointerDate.AddDays(1);

}

}

And then simply use with the following code:

int leavesApplied = allDates.Count(day => day.WeekDay()).

Hope this helps.