Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Calculating working days between two date and time fields by excluding Business holidays and weekends

$
0
0

Hi,

I am trying to find the no of working days between two date time fields by using the below code but my code is failing at some tests where if I keep my start date as 25/08/2017 00:00 and end date as 28/08/2017 23:59 I am getting working dates as 2 days. Please suggest me how I can achieve it.

public int AddWorkingAndBusinessClosureDays(DateTime startDate, bool excludeWeekend, IOrganizationService service, DateTime endDate, IWorkflowContext context)
        {
            IEnumerable<Entity> closureCalendarRules = GetBusinessClosureCalendarRules(context, service);
            int num1 = Convert.ToInt32(endDate.Subtract(startDate).TotalDays);
            int num2 = Convert.ToInt32(endDate.Subtract(startDate).TotalMinutes);
            if (excludeWeekend)
            {
                for (DateTime date = startDate; date.Date <= endDate.Date; date = date.AddDays(1.0))
                {
                    if (!IsWorkingDayOfWeek(date))
                    {
                        if (num1 >= 1)
                        {
                            --num1;
                            num2 -= 1440;
                        }
                        else
                        {
                            num1 = 0;
                            num2 = 0;
                        }
                    }
                }
            }
            if (num2 > 0)
            {
                IEnumerable<Entity> source = closureCalendarRules.Where(bc =>
                {
                    if (bc.GetAttributeValue<DateTime>("effectiveintervalstart") >= startDate && bc.GetAttributeValue<DateTime>("effectiveintervalstart") <= endDate && bc.GetAttributeValue<DateTime>("effectiveintervalend") >= startDate)
                        return bc.GetAttributeValue<DateTime>("effectiveintervalend") <= endDate;
                    return false;
                });
                if (source != null && source.Count() > 0)
                {
                    int num3 = 0;
                    using (IEnumerator<Entity> enumerator = source.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            Entity current = enumerator.Current;
                            DateTime date1 = current.GetAttributeValue<DateTime>("effectiveintervalstart");
                            DateTime dateTime = current.GetAttributeValue<DateTime>("effectiveintervalend");
                            int num4 = Convert.ToInt32(current.GetAttributeValue<int>("duration"));
                            if (num4 >= 1440 )
                            {
                                if (excludeWeekend)
                                {
                                    for (DateTime date2 = date1; date2 <= dateTime; date2 = date2.AddDays(1.0))
                                    {
                                        if (!IsWorkingDayOfWeek(date2))
                                            num4 -= 1440;
                                    }
                                }
                            }
                            else if (excludeWeekend && !IsWorkingDayOfWeek(date1))
                                num4 = 0;
                            num3 += num4;
                        }
                    }
                    num2 -= num3;
                }
            }
            if (num2 < 0)
                num2 = 0;
            return num2;
        }
        /// <summary>
        /// Get Business Closure Calendar Rules
        /// </summary>
        private  IEnumerable<Entity> GetBusinessClosureCalendarRules(IWorkflowContext context, IOrganizationService service)
        {
            // Get Organization Business Closure Calendar Id
            var organization = service.Retrieve("organization", context.OrganizationId, new ColumnSet("businessclosurecalendarid"));

            var query = new QueryExpression("calendar")
            {
                ColumnSet = new ColumnSet(true),
                Criteria = new FilterExpression()
            };

            // Add condition to get Get Calander where CalanderId is equal to Organization's businessclosurecalendarid
            query.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.Equal, organization["businessclosurecalendarid"].ToString()));

            // Get Calendar
            var businessClosureCalendar = service.RetrieveMultiple(query).Entities[0];
            return businessClosureCalendar == null || businessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules") == null ? null : businessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules").Entities;
        }


        private bool IsWorkingDayOfWeek(DateTime date)
        {
            DayOfWeek dayOfWeek = date.DayOfWeek;
            return dayOfWeek != DayOfWeek.Saturday && (uint)dayOfWeek > 0U;
        }       



Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images