title: day minus one method to calculate business day c#
body: <p>I've been working on a project where a date is calculated based on a number of days added to current DateTime.Now. I'm using several functions based on this work: <a href="http://tinyurl.com/884kojy" rel="nofollow">business dates calculation</a> , and since this requires a calculation of business days (no weekend days or holidays are calculated, holidays are coded to an app config file), the count needs to start on the current date "now", or the previous business day if start is on a weekend day or holiday. The <em>current</em> class file always moves <em>ahead</em> to the next business day before starting the count. If the start date is today, the count will start tomorrow.</p>
  
  <p>My requirements for example: a start day of 1/21/2012 is a Saturday. A value of 10 days is added. The resulting date <em>should be</em> February 2. This means the business day to start a calculation is Friday January 20 + 9 more days = 10 days. Another example would be to set the start on Sunday January 22, stepping back to January 20, and add 9 days with the same result. Provided The count does not start on weekends or holidays, it should skip them and arrive at the next calculated business day.</p>
  
  <p>The issue I'm having is my project needs to calculate based on the current date, or step back to the previous business day if the count start is a weekend or holiday.</p>
  
  <p>I think the class file is very clear, but I cant seem to find a way to force the count to start on the current business day, or step backward to the previous business day if the count start is a weekend or holiday. </p>
  
  <p>Please have a look at the code in the link. I've attempted to add negative values to the addDays param in several of the existing methods; in my form I've tried to force the dateTimePicker value to subtract a day, and a variety of other thrashings about throughout the class file, resulting in failure.</p>
  
  <p>Any advice or help would be greatly appreciated!</p>
  
  <p>Code in button click event:</p>
  
  <pre><code> //DateTime.Today.AddDays(-4);
  
           DateTime date = dateTimePicker1.Value;
          // dateTimePicker1.Value.AddDays(-2);
          XDateTime sDate = new XDateTime(dateTimePicker1.Value.ToShortDateString(), XDateTimeType.Business);
  
          string Str = textBox2.Text.Trim();
          string Str2 = textBox3.Text.Trim();
  
          double Num;
          bool isNum = double.TryParse(Str, out Num);
          bool isNum2 = double.TryParse(Str2, out Num);
          label6.Text = "";
  
          //Test whether textBox contains a number or some other character. Fails if not a number.
          if (isNum)
          {
              short days = Convert.ToInt16(textBox2.Text);
              sDate.AddBusinessDays(days);
  
              lblTodaysDate.Text = " Substantial Completion Date: " + sDate.Date.ToString("MMMM d, yyyy");
              label3.Text = "";
  
              if (isNum2)
              {
                  short days2 = Convert.ToInt16(textBox3.Text);
                  sDate.AddBusinessDays(days2);
                  label6.Text = " Physical Completion Date: " + sDate.Date.ToString("MMMM d, yyyy");
                  label5.Text = "";
              }
              else
                  label5.Text = "Please enter numbers only!";             
              return;
          }
          else
             label3.Text = "Please enter numbers only!";
          return;
  </code></pre>