Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:59:54+00:00 2026-06-14T09:59:54+00:00

I have seen numerous posts related to adding working days (excluding weekends) to a

  • 0

I have seen numerous posts related to adding working days (excluding weekends) to a date parameter and I tried using a few from those. However, all the solutions I picked are failing in some scenarios.
below is the code I’m currently using:

private DateTime AddWorkingDays(DateTime dateValue, int noOfDays) 
    {
        // determine if we are adding or subtracting the days
        int nDirection = noOfDays < 0 ? -1 : 1;

        // move ahead the day of week
        int nWeekday = noOfDays % 5;
        while (nWeekday != 0)
        {
            dateValue = dateValue.AddDays(nDirection);
            if (dateValue.DayOfWeek != DayOfWeek.Saturday
                && dateValue.DayOfWeek != DayOfWeek.Sunday)
            {
                nWeekday -= nDirection;
            }
        }

        // move ahead the number of weeks
        int nDayweek = (noOfDays / 5) * 7;
        dateValue = dateValue.AddDays(nDayweek);

        return dateValue;
    } 

Example scenarios that fail:
Date: 24 Nov, 2012(saturday), No of days: 5 (or 10)
Result: 1 Dec, 2012 (or 8 Dec, 2012) when the expected result was 30 Nov, 2012
Fails for 25 Nov, 2012 also.. and similar scenarios I guess, where the input date falls on a weekend.

Can someone please help fixing such scenarios ? Or provide a better solution ?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:59:55+00:00Added an answer on June 14, 2026 at 9:59 am

    This implementation (actually quite similar to yours) gives the results you expect:

        public static DateTime AddWorkingDays(this DateTime date, int days)
        {
            if (days == 0)
                return date;
            int sign = days < 0 ? -1 : 1;
            while (days % 5 != 0 || !date.IsWorkingDay())
            {
                date = date.AddDays(sign);
                if (!date.IsWorkingDay())
                    continue;
                days -= sign;
            }
            int nWeekEnds = days / 5;
            DateTime result = date.AddDays(days + nWeekEnds * 2);
            return result;
        }
    
        public static bool IsWorkingDay(this DateTime date)
        {
            return !(date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen numerous posts about the IE Webpage has expired error message, but
I have seen numerous posts on this and numerous answers, but I can't quite
I have seen numerous ways to highlight the current tab, but they all have
I have seen numerous questions on MVP that mostly revolve around the View and
I have seen in a few libraries, Spray for example, dual package specifications like
I have seen a few design where the developers have defined very long values
I've seen numerous posts in random places about how to set up a WYSIWYG
I have reviewed numerous reports of this error but the solutions I have seen
I have seen numerous answers on here about creating an intent filter for a
I have seen numerous examples on some SharePoint blogs regarding the use of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.