Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

Hi,

I am trying to find the no of working days between two date time fields. Please suggest me how I can achieve it.

                    if (!IsWorkingDayOfWeek(date))
                    {
                        if (num1 >= 1)
                        {
                            --num1;
                            num2 -= 1440;
                        }
                        else
                        {
                            num1 = 0;
                            num2 = 0;
                        }
                    }
        
        /// <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;
        }       

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculating working days between two date and time fields by excluding Business holidays and weekends

    Hi Siraj,

    Many Thanks for replying to my query. I have removed not in the if statement and this time I am facing different issue.

  • Suggested answer
    Siraj M Profile Picture
    Siraj M on at
    RE: Calculating working days between two date and time fields by excluding Business holidays and weekends

    with the below statements it looks like you are counting only saturdays and sundays between the two dates

    if (!IsWorkingDayOfWeek(date))

    return dayOfWeek != DayOfWeek.Saturday && (uint)dayOfWeek > 0U;

    you need to remove not in the If statement

    Regards,
    Siraj

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans