×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# algorithm for this code

## algorithm for this code

(OP)
If I pass a date and the days that should be subtracted I need to find the next weekday. Here is what I have:

#### CODE

static DateTime GetFileDate(DateTime BusDate, int DayLag)
{
DateTime FileDate;
FileDate = DateTime.Today;
switch (BusDate.DayOfWeek.ToString())
{
case "Monday":
break;
case "Tuesday":
if (DayLag == -3)
else if (DayLag == -2)
else if (DayLag == -1)
break;
case "Wednesday":
if (DayLag == -3)
if (DayLag == -2)
if (DayLag == -1)
break;
default:
break;

}

return FileDate;
} 

So Monday I've figured out it should be DayLag - 3. If I pass DayLag = 1 and BusDate is 9/23 then FileDate is 9/20. I can't figure out for the other days.

### RE: algorithm for this code

Can you give us a working example in dates. For example in 2013, for

16/9 result is 17/9
17/9 result is 18/9
...
20/9 result is 23/9
21/9 result is 23/9
22/9 result is 23/9
23/9 result is 24/9

Is this what you want or is it something completely different. Also, what is this lag you are talking about?

### RE: algorithm for this code

If I'm understand what you're asking for correctly something like this should work...

#### CODE --> C#

static DateTime GetFileDate(DateTime busDate, int dayLag)
{
int daysSubtracted = 0;
while (daysSubtracted > dayLag)
{
if (busDate.DayOfWeek != DayOfWeek.Sunday && busDate.DayOfWeek != DayOfWeek.Saturday)
daysSubtracted++;
}
return busDate;
} 

### RE: algorithm for this code

I wish you could delete posts as I sit here face palming...

Should be:

#### CODE --> C#

static DateTime GetFileDate(DateTime busDate, int dayLag)
{
int daysSubtracted = 0;
while (daysSubtracted < dayLag)
{
if (busDate.DayOfWeek != DayOfWeek.Sunday && busDate.DayOfWeek != DayOfWeek.Saturday)
daysSubtracted++;
}
return busDate;
} 

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!