RE: Calculating working days from two dates by looking into business closures and weekends
Another option could be to calculate total number of day between two dates using
TimeSpan dateDiff = dateTime1 - dateTime2;
var days = dateDiff.TotalDays;
And then you can use looping to add day one by one to first date and pass it to below method to check if it's holiday.
private bool IsHoliday(DateTime date, IOrganizationService service)
{
DateTime tempClosureDate;
QueryExpression query = new QueryExpression("calendar");
query.ColumnSet = new ColumnSet(true);
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "name";
condition.Operator = ConditionOperator.Equal;
condition.Values.Add("Business Closure Calendar");
query.Criteria.Conditions.Add(condition);
EntityCollection calendars = service.RetrieveMultiple(query);
EntityCollection calendarrule = calendars[0].GetAttributeValue<EntityCollection>("calendarrules");
foreach (Entity e in calendarrule.Entities)
{
tempClosureDate = (DateTime)e["starttime"];
if (tempClosureDate.ToString("d").Equals(date.ToString("d")))
{
return true;
}
}
return false;
}
}
Hope it will help you.