gary7
gary7

Reputation: 61

day minus one method to calculate business day c#

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: business dates calculation , 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 current class file always moves ahead to the next business day before starting the count. If the start date is today, the count will start tomorrow.

My requirements for example: a start day of 1/21/2012 is a Saturday. A value of 10 days is added. The resulting date should be 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.

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.

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.

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.

Any advice or help would be greatly appreciated!

Code in button click event:

 //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;

Upvotes: 0

Views: 1717

Answers (2)

gary7
gary7

Reputation: 61

The modification to the method below results in the count starting on the selected day rather than the next business. This resolves the issue. Thank you to all who commented!

public void AddBusinessDays(short days)
    {
        double sign = Convert.ToDouble(Math.Sign(days));
        int unsignedDays = Math.Sign(days) * days -1; //*days; added -1 to force count to start on selected day, rather than next business day.
        for (int i = 0; i < unsignedDays; i++)
        {
            do
            {
                _date = _date.AddDays(sign);
            }
            while (!this.IsWorkDay);
        }
    }

Upvotes: 0

arx
arx

Reputation: 16896

Did you read the article you link to?

It contains the following snippet for calculating the previous business day:

do 
{
   date = date.AddDays(-1.0);
}
while (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday || 
    _holidays.ContainsValue(date.ToString(_format)));

It also contains a method for forcing the date to be a business day, which chooses the next business day:

private void check()
{
   if (_type == XDateTimeType.Business && !this.IsWorkDay)
   {
      _date = this.NextBusinessDay();
   }
}

If you consistently want to start from the previous day, change the check method to call PreviousBusinessDay. Or create an analogous checkPrevious method.

Upvotes: 2

Related Questions