Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

algorithm for this code

Status
Not open for further replies.

sds814

Programmer
Feb 18, 2008
164
0
0
US
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.
 
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?
 
If I'm understand what you're asking for correctly something like this should work...

Code:
[COLOR=#0000FF]static[/color] [COLOR=#2B91AF]DateTime[/color] GetFileDate([COLOR=#2B91AF]DateTime[/color] busDate, [COLOR=#0000FF]int[/color] dayLag)
{
    [COLOR=#0000FF]int[/color] daysSubtracted = 0;
    [COLOR=#0000FF]while[/color] (daysSubtracted > dayLag)
    {
        busDate = busDate.AddDays(-1);
        [COLOR=#0000FF]if[/color] (busDate.DayOfWeek != [COLOR=#2B91AF]DayOfWeek[/color].Sunday && busDate.DayOfWeek != [COLOR=#2B91AF]DayOfWeek[/color].Saturday)
            daysSubtracted++;
    }
    [COLOR=#0000FF]return[/color] busDate;
}
 
I wish you could delete posts as I sit here face palming...

Should be:

Code:
[COLOR=#0000FF]static[/color] [COLOR=#2B91AF]DateTime[/color] GetFileDate([COLOR=#2B91AF]DateTime[/color] busDate, [COLOR=#0000FF]int[/color] dayLag)
{
    [COLOR=#0000FF]int[/color] daysSubtracted = 0;
    [COLOR=#0000FF]while[/color] (daysSubtracted < dayLag)
    {
        busDate = busDate.AddDays(-1);
        [COLOR=#0000FF]if[/color] (busDate.DayOfWeek != [COLOR=#2B91AF]DayOfWeek[/color].Sunday && busDate.DayOfWeek != [COLOR=#2B91AF]DayOfWeek[/color].Saturday)
            daysSubtracted++;
    }
    [COLOR=#0000FF]return[/color] busDate;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top