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#?
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:
What is a good way to do this in C#?
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;
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
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);
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
// 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;
}