-1

I am wondering if i can get the date of every alternate friday starting with 13th of April, 2012 to give it as a parameter to a stored procedure using c#, asp.net?

It should also be most recently passed date. Thank you!

user1270384
  • 711
  • 7
  • 24
  • 53
challengeAccepted
  • 7,106
  • 20
  • 74
  • 105

4 Answers4

8

Just set a DateTime with the date you want to start at, and then keep adding 14 days:

So to get every other Friday after 4/13 until the end of the year:

DateTime dt = new DateTime(2012, 04, 13);
while (dt.Year == 2012)
{
    Console.WriteLine(dt.ToString());
    dt = dt.AddDays(14);
}

More info after comment:

If you want the most recent alternate Friday since 2012/04/13, you can compute the number of days between now and 2012/04/13, take the remainder of that divided by 14, and subtract that many days from today's date:

DateTime baseDate = new DateTime(2012, 04, 13);
DateTime today = DateTime.Today;

int days = (int)(today - baseDate).TotalDays;
int rem = days % 14;
DateTime mostRecentAlternateFriday = today.AddDays(-rem);
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
2

You can easily make a generator method that would give you the set of fridays:

public IEnumerable<DateTime> GetAlternatingFridaysStartingFrom(DateTime startDate)
{
    DateTime tempDate = new DateTime(startDate.year, startDate.Month, startDate.Day);

    if(tempDate.DayOfWeek != DayOfWeek.Friday)
    {
        // Math may be off, do some testing
        tempDate = tempDate.AddDays((7 - ((int)DayOfWeek.Friday - (int)tempDate.DayOfWeek) % 7);
    }

    while(true)
    {                
        yield return tempDate;
        tempDate = tempDate.AddDays(14);
    }
}

Then, simply use some LINQ to determine how much you want:

var numberOfFridays = GetAlternatingFridaysStartingFrom(DateTime.Today).Take(10);
Tejs
  • 40,736
  • 10
  • 68
  • 86
  • DateTime is a struct, and therefore values are copied. There is no need for a temp date; you can just use the parameter; If you want it in a new variable just for a different name, just assign startDate to TempDate and it will perform a deep copy. Also, more readable way to get to the next friday is `while(tempDate isn't friday)tempDate.AddDays(1)` It doesn't perform quite as well, but it's still constant time so who cares. – Servy Apr 17 '12 at 19:06
  • Potentially, but someone could pass a date time with minutes and seconds information, and I didnt want to copy that forward. This way, you just get a XX/YY/ZZ 00:00.000 datetime. – Tejs Apr 17 '12 at 19:08
  • You can use the `Date` property for that. `tempDate = startDate.Date` Then it makes the intention clearer in code. – Servy Apr 17 '12 at 19:10
  • Possible way to use this method: `var dates = GetAlternatingFridaysStartingFrom(startingDate).TakeWhile(date => date < endingDate);` – Servy Apr 17 '12 at 19:11
0

Why do you need a stored proc?

If you have a date that is Friday, why not just use AddDays(14) in a loop?

If you want to find the nearest Friday from a start date, just use this:

while(date.DayOfWeek != DayOfWeek.Friday)
{
    date.AddDays(1);
}

Then use the 14 day loop to get every other Friday.

SouthShoreAK
  • 4,176
  • 2
  • 26
  • 48
0

You can create simple method that will enumerate them like so:

public static IEnumerable<DateTime> GetAlternatingWeekDay(DateTime startingDate)
{
    for (int i = 1; ; i++)
    {
        yield return startingDate.AddDays(14*i);
    }
}

Which you can call like this:

DateTime startingDate = DateTime.Parse("2012-04-13");
foreach (var date in GetAlternatingWeekDay(startingDate).Take(10))
{
    Console.WriteLine(date.ToString("R"));
}

Alternately, if you need to know the date for a given number of weeks out, you could use code like this:

DateTime date = DateTime.Parse("2012-04-13").AddDays(7 * numberOfWeeks);
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60