2

Working in Visual Studio with C#. I'm making a piece of software for a small office that captures lawsuits. Once a lawsuit is captured, the office has 15 days to send some documents to the tribunal. So deadLine is in 15 days.

But if there is for example, and update (update1) to the lawsuit, the office now has a new deadLine to send update1 documents.

So, deadLine for Capture is dd/MM/yyy Then, deadLine for update1 is dd/MM/yyyy

But I need to exclude all non labor days, so the deadLines do not display any non labor days. Example:

Capture: 15 days Update1: 5 days Update2: 10 days Update3: 7 days

So, if I capture today, July 28 2011, 15 days should be added to the deadLine variable, which would be August 12, but I have to exclude non labor days from a calendar. For example August 12 2011.

I've turned the DatePicker value to string, with format (dd/MM/yyyy), and I'm comparing it to a list of strings of non-valid days, with format (dd/MM/yyyy).

And if the deadLine falls on one of these days, I convert deadLine to dateTime, add 1 day, until it does not fall on one of these days. Then save the final dateTime value as the new deadLine.

Example:

string DeadLine;
string NonValidDay001 = 12/08/2011;
string NonValidDay001 = 15/08/2011;
string NonValidDay001 = 19/08/2011;

DeadLine = DatePicker01.Value.AddDays(15).ToString("dd/MM/yyyy");

while (DeadLine == NonValidDay001 | DeadLine == NonValidDay001 | DeadLine == NonValidDay001)
{
    dateTime dt = Convert.toDateTime(DeadLine);
    dt.AddDays(1).ToString("dd/MM/yyyy");
    DeadLine = dt;
}

The problem I'm having is, it is not adding days to the dealine if deadline matches non-valid days.

What I'm doing is not working in any way.

Could anyone enlighten me please. (=

Yushell
  • 745
  • 1
  • 7
  • 16
  • Do you also want it to skip over labour days if the appear within the 15 days? (not just if the date 15 days from the start is a labour day) – George Duckett Jul 28 '11 at 09:47

3 Answers3

4

You need to change the following...

dt.AddDays(1).ToString("dd/MM/yyyy");
DeadLine = dt;

to...

DeadLine = dt.AddDays(1).ToString("dd/MM/yyyy");

"The method AddDays does not change the value of the DateTime. Instead, it returns a new DateTime whose value is the result of the AddDays operation."

For more info on a function for adding work days from a certain date see the following...

C#: Adding working days from a cetain date

Community
  • 1
  • 1
swuk
  • 883
  • 6
  • 14
  • This helped alot. Thanks. But I still have a problem. Just as George Duckett said. I do want to include non labor days within the 15 days. This helped with the AddDays issue anyway (= – Yushell Aug 01 '11 at 07:32
  • No prob, I've updated the answer with some more info that may help you. – swuk Aug 01 '11 at 09:11
0

The line

dt.AddDays(1).ToString("dd/MM/yyyy");

doesn't achieve anything. DateTimes are immutable. All that line does is create a new DateTime and a new String that are never referenced. I think you mean:

DeadLine = dt.AddDays(1).ToString("dd/MM/yyyy");

However, it seems a little strange that you're using Strings rather than just DateTimes.

Alex Humphrey
  • 6,099
  • 4
  • 26
  • 41
0

If you wanted to skip over labour days within the time period (i.e. within the 15 days) then you could use the following (untested, typed straight in here):

public DateTime AddDaysExcluding(DateTime startDate, int days, params DateTime[] excludedDays)
{
    while(days != 0)
    {
        if (excludedDays.Contains(startDate))
            startDate = startDate.AddDays(1);

        startDate = startDate.AddDays(1);
        days--;
    }

    if (excludedDays.Contains(startDate))
        startDate = startDate.AddDays(1);
}

Example usage:

DateTime deadline = AddDaysExcluding(DatePicker01.Value, 15,
    new DateTime(2011, 8, 12), new DateTime(2011, 8, 15), new DateTime(2011, 8, 19));
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • I don't understand the code very well. But I could manage to use it. The problem is. How would I implement this in my code? And yes. I do need skip labor days within the time period. (= – Yushell Aug 01 '11 at 07:35
  • You would add this method somewhere, then call it, passing in your start date, the number of days, then all your labour days, either as an array, or as a list of params. See here: http://msdn.microsoft.com/en-us/library/w5zay9db(v=vs.100).aspx – George Duckett Aug 01 '11 at 08:16