INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Jobs

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":
                    FileDate = BusDate.AddDays(DayLag - 3);
                    break;
                case "Tuesday":
                    if (DayLag == -3)
                        FileDate = BusDate.AddDays(-5);
                    else if (DayLag == -2)
                        FileDate = BusDate.AddDays(-4);
                    else if (DayLag == -1)
                        FileDate = BusDate.AddDays(-1);
                    break;
                case "Wednesday":
                    if (DayLag == -3)
                        FileDate = BusDate.AddDays(-5);
                    if (DayLag == -2)
                        FileDate = BusDate.AddDays(-2);
                    if (DayLag == -1)
                        FileDate = BusDate.AddDays(-1);
                    break;
                default:
                    FileDate = BusDate.AddDays(DayLag);
                    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.

Please help.

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)
    {
        busDate = busDate.AddDays(-1);
        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)
    {
        busDate = busDate.AddDays(-1);
        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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close