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 :
Finance | Project Operations, Human Resources, ...
Answered

Want to calculate days in a week except Saturday and Sunday in between selected dates.

(1) ShareShare
ReportReport
Posted on by 518

Hi All,

Want to calculate days in a week except Saturday and Sunday in between selected dates.

Suppose i have selected Start date (08/07/2020) and end date (27/07/2020) so the number of days in between is 20 days but leaving Saturday and Sunday its 15 days.

Can any one help with the code for this.

Thank you

Siddhant Singh

I have the same question (0)
  • Verified answer
    Jay Barot Profile Picture
    1,502 on at

    Hi Siddhant,

    If you have work calendar setup on your machine then you can use the below link to get week days.

    community.dynamics.com/.../how-to-calculate-how-many-saturday-sunday-in-a-particular-date-range

    Please go through it and let us know the status.

  • Jay Barot Profile Picture
    1,502 on at

    And if there is no calendar defined the follow below path to create the same.  Go to Organisation Administration >> Common >> Calendars >>Calendars.

  • WillWU Profile Picture
    22,361 on at

    Hi Siddhant Singh,

    You could use the WorkCalendarSched class.

    static void WorkingdaysInPeriod()
    
    {
    
       WorkCalendarSched workCalendarSched;
       date start;
       date end;
       counter workingdays;
       counter totaldays;
       CalendarId primCalendar="myCalendar";
       ;
    
       start=str2date("08-07-2020",123);
       end= str2Date("27-07-2020",123);
    
        workCalendarSched = new workCalendarSched();
    
        while(start

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    If you don't use work calendar, you can simply iterate through all the dates and use the dayOfWk(date) function to extract the day of week.

    Or, for better performance, check the dayOfWk of the start date and dayOfWk of the end date and figure out how many full weeks there must be in between. I'm not going to write the code for you but it should be quite easy.

    Also DateTimeUtil::getDifference can be used to get the difference of two dates.

    The logic would be something like this:

    - How many weekdays (Mon-Fri) are there from the start date until end of that week?

    - How many weekdays (Mon-Fri) are there from the end date until the beginning of that week?

    - How many full weeks are in between the two dates? Nr of weeks * 5 is the nr of working days.

    Then combine the results of those three calculations.

    So, depending on the requirement I would use "dummy iteration" (if the difference between the start and end date is always small) or write a more clever algorithm (if the code needs to handle difference of many years).

  • HenryXie Profile Picture
    on at

    Hi Siddhant,  use calendar will be better. X++ pls reference below link

    https://dynamicsuser.net/ax/f/developers/49484/x-coding-how-to-calculate-the-working-days

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    It's also worth pointing out that even if work calendar is used, checking open days of the calendar and checking if the day is Mon-Fri are two different things. In the work calendar you might have public holidays and other closed days even on the weekdays.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans