5

I need to detect if the current day is the third friday in the last month of the quarter.

For 2012 that would be these four dates:

  • 2012-03-16
  • 2012-06-15
  • 2012-09-21
  • 2012-12-21

What is a good way to do this in C#?

numaroth
  • 1,295
  • 4
  • 25
  • 36
Koralek M.
  • 3,231
  • 4
  • 26
  • 32
  • possible duplicate of [How to calculate 2nd Friday of Month in C#](http://stackoverflow.com/questions/6140018/how-to-calculate-2nd-friday-of-month-in-c-sharp) – H H May 03 '12 at 07:26
  • 1
    possible duplicate of [How to find the 3rd Friday in a month with C#?](http://stackoverflow.com/questions/5421972/how-to-find-the-3rd-friday-in-a-month-with-c) – Stefan May 03 '12 at 07:28
  • So if the date is 2012-06-15 is that a match? Or is it 2012-05-18 (month before end) that would be the match? – Mathew Thompson May 03 '12 at 07:50
  • @Stefan that's not exactly a duplicate, there's additional logic needed here – Mathew Thompson May 03 '12 at 08:08

5 Answers5

2

Or, you can go without any loops and simply assume today is 3rd friday and find what day it was 2 weeks ago (should be friday of the same month for positive match):

var now = DateTime.UtcNow;
var firstFriday = now.AddDays(-14);
return now.Month % 3 == 0
    && firstFriday.DayOfWeek == DaysOfWeek.Friday
    && now.Month == firstFriday.Month;
k.m
  • 30,794
  • 10
  • 62
  • 86
  • 1
    There can be up to five fridays in a month. One way of checking is to add a check that now.AddDays(-21) isn't the same month. – Fredrik Ljung May 03 '12 at 07:49
1

Well you can start off with the first day of that month and advance till you find the first Friday, post that you can add 14 days to arrive at the third friday

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

Using the extension method written by Bert Smith in This Answer Here is the method IsThirdFridayInLastMonthOfQuarter Which will do exactly what you're looking for:

public static class DateHelper
{
    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));
    }

    public static bool IsThirdFridayInLastMonthOfQuarter(DateTime date)
    {
        // quarter ends
        int[] months = new int[] { 3, 6, 9, 12 };

        // if the date is not in the targeted months, return false.
        if (!months.Contains(date.Month))
            return false;

        // get the date of third friday in month
        DateTime thirdFriday = date.NthOf(3, DayOfWeek.Friday);

        // check if the date matches and return boolean
        return date.Date == thirdFriday.Date;
    }
}

To use it:

bool isThirdFriday = DateHelper.IsThirdFridayInLastMonthOfQuarter(date);
Community
  • 1
  • 1
0

You can use the Time Period Library for .NET:

// ----------------------------------------------------------------------
public DateTime? GetDayOfLastQuarterMonth( DayOfWeek dayOfWeek, int count )
{
  Quarter quarter = new Quarter();
  Month lastMonthOfQuarter = new Month( quarter.End.Date );

  DateTime? searchDay = null;
  foreach ( Day day in lastMonthOfQuarter.GetDays() )
  {
    if ( day.DayOfWeek == dayOfWeek )
    {
      count--;
      if ( count == 0 )
      {
        searchDay = day.Start.Date;
        break;
      }
    }
  }
  return searchDay;
} // GetDayOfLastQuarterMonth

Now you do your check:

// ----------------------------------------------------------------------
public void CheckDayOfLastQuarterMonth()
{
  DateTime? day = GetDayOfLastQuarterMonth( DayOfWeek.Friday, 3 );
  if ( day.HasValue && day.Equals( DateTime.Now.Date ) )
  {
    // do ...
  }
} // CheckDayOfLastQuarterMonth
0
// Do a few cheap checks and ensure that current month is the last month of 
// quarter before computing the third friday of month
if (Cur.DayOfWeek == DayOfWeek.Friday && Cur.Day > 14 && Cur.Month % 3 == 0) {
    var Friday = new DateTime(Cur.Year, Cur.Month, 15);
        Friday = Friday.AddDays((5 - (int)Friday.DayOfWeek + 7) % 7);
    if (Cur.Day == Friday.Day)
        return true;
}
chansen
  • 2,446
  • 15
  • 20