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 between two date and time fields by excluding Business holidays and weekends

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Siraj M Profile Picture
    on at

    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
  • Community Member Profile Picture
    on at

    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.

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