10

Possible Duplicate:
How to find the 3rd Friday in a month with C#?

Hi everyone,

I've wrote a little console utility that spits out a line into a text file. I want this line to include the second Friday of the current month. Is there any way to do this?

Thanks everyone!

Community
  • 1
  • 1
New Start
  • 1,401
  • 5
  • 21
  • 29

4 Answers4

19

Slight variation on @druttka: using an extension method.

 public static DateTime NthOf(this DateTime CurDate, int Occurrence , DayOfWeek Day)
 {
     var fday = new DateTime(CurDate.Year, CurDate.Month, 1);

     var fOc = fday.DayOfWeek == Day ? fday : fday.AddDays(Day - fday.DayOfWeek);
     // CurDate = 2011.10.1 Occurance = 1, Day = Friday >> 2011.09.30 FIX. 
     if (fOc.Month < CurDate.Month) Occurrence = Occurrence+1;
     return fOc.AddDays(7 * (Occurrence - 1));
 }

Then called it like this:

 for (int i = 1; i < 13; i++)
 {
      Console.WriteLine(new DateTime(2011, i,1).NthOf(2, DayOfWeek.Friday));
 }
Community
  • 1
  • 1
Bert Smith
  • 701
  • 3
  • 11
  • +1 but IMO `fOc` is bad variable name. I had to think what on earth does `fOc` stand for? I got it but it wasted a few seconds. Since we read code more than we write it I think it's better to spell it out. – User Sep 11 '13 at 20:46
  • 1
    Possible bug: looks like (fOc.Month < CurDate.Month) will not give right answer when fOc crosses the year boundary (i.e. goes from January to December) – User Sep 11 '13 at 20:59
  • @User what on earth does fOc stand for? – PandaWood Jun 15 '18 at 05:28
  • @PandaWood I believe it stands for "first occurrence" (of the desired day of week). – Adam Shaver Oct 12 '18 at 23:09
  • 1
    @User I plugged this in to some unit tests and I am seeing a bug when crossing year boundaries. I added the equivalent to `|| fOc.Year < CurDate.Year` to the `if` and so far this has resolved my unit tests that were failing. – Adam Shaver Oct 12 '18 at 23:14
2

I would go for something like this.

    public static DateTime SecondFriday(DateTime currentMonth)
    {
        var day = new DateTime(currentMonth.Year, currentMonth.Month, 1);
        day = FindNext(DayOfWeek.Friday, day);
        day = FindNext(DayOfWeek.Friday, day.AddDays(1));
        return day;
    }

    private static DateTime FindNext(DayOfWeek dayOfWeek, DateTime after)
    {
        DateTime day = after;
        while (day.DayOfWeek != dayOfWeek) day = day.AddDays(1);
        return day;
    }
vidstige
  • 12,492
  • 9
  • 66
  • 110
0

Untested, but this should grab it.

DateTime today = DateTime.Today;
DateTime secondFriday = 
     Enumerable.Range(8, 7)
               .Select(item => new DateTime(today.Year, today.Month, item))
               .Where(date => date.DayOfWeek == DayOfWeek.Friday)
               .Single();
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
0

fully tested:

for (int mo = 1; mo <= 12; mo++)
{
    DateTime _date = new DateTime(yr, mo, 1);
    DayOfWeek day = _date.DayOfWeek;

    int d = 0;
    if (day == DayOfWeek.Saturday)
        d += 7;

    var diff = DayOfWeek.Friday - day;

    DateTime secFriday = _date.AddDays(diff + 7 + d);
    Console.WriteLine(secFriday.ToString("MM\tddd\tdd"));
}  

Final results:

Month           Date
=====================
01      Fri     14
02      Fri     11
03      Fri     11
04      Fri     08
05      Fri     13
06      Fri     10
07      Fri     08
08      Fri     12
09      Fri     09
10      Fri     14
11      Fri     11
12      Fri     09
IAbstract
  • 19,551
  • 15
  • 98
  • 146