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

  • Suggested answer
    nmaenpaa Profile Picture
    101,158 Moderator on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

    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.

  • HenryXie Profile Picture
    on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

    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,158 Moderator on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

    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).

  • WillWU Profile Picture
    22,352 on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

    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

  • Jay Barot Profile Picture
    1,502 on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

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

  • Verified answer
    Jay Barot Profile Picture
    1,502 on at
    RE: Want to calculate days in a week except Saturday and Sunday in between selected dates.

    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.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,281 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,019 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans