Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Calculating working days from two dates by looking into business closures and weekends

Posted on by Microsoft Employee

Hi Experts,

I have quick query on how to calculate the working days between two dates by excluding the business closure days and weekends. Could someone shed some light how to calculate the difference between two dates. Thanks for your help. I have seen few articles but most of them are like adding no of working days to the current date or created on date.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculating working days from two dates by looking into business closures and weekends

    Hi Bellam,

    I am facing a similar case. Could you please guide me with your solution? Thanks in advance.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculating working days from two dates by looking into business closures and weekends

    Many Thanks for your help. I achieved it in a different way.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculating working days from two dates by looking into business closures and weekends

    Hi HIMBAP,

    Many Thanks again for your time in answering my query. 

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Calculating working days from two dates by looking into business closures and weekends

    Hi Mahen,

    Thank you for replying to my query but I have already see the link which you have provided where it is adding working days to created on date. Here I am looking to find the difference between two dates by excluding the business closure days and week ends. Any ideas...? Could you please shed some light.

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Calculating working days from two dates by looking into business closures and weekends

    Hi,

    Did you check this one

    hayercrm.blogspot.in/.../dynamics-crm-how-to-add-working-days-by.html

    Thanks

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans