web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    Did you check this one

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

    Thanks

  • Community Member Profile Picture
    on at

    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.

  • Mahendar Pal Profile Picture
    45,095 on at

    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
    on at

    Hi HIMBAP,

    Many Thanks again for your time in answering my query. 

  • Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

    Hi Bellam,

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans