Sử dụng phương pháp mở rộng được viết bởi Bert Smith trong This Answer Dưới đây là phương pháp IsThirdFridayInLastMonthOfQuarter
Điều đó sẽ làm chính xác những gì bạn đang xem ing cho:
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;
}
}
Để sử dụng nó:
bool isThirdFriday = DateHelper.IsThirdFridayInLastMonthOfQuarter(date);
Nguồn
2012-05-03 09:05:56
có thể trùng lặp của [Cách tính Thứ Sáu thứ 2 của Tháng trong C#] (http://stackoverflow.com/questions/6140018/how-to-calculate-2nd-friday-of-month-in-c-sharp) –
có thể bản sao của [Làm thế nào để tìm thứ Sáu thứ 3 trong một tháng với C#?] (http://stackoverflow.com/questions/5421972/how-to-find-the-3rd-friday-in-a-month-with-c) – Stefan
Vì vậy, nếu ngày là 2012-06-15 là một trận đấu? Hay là 2012-05-18 (tháng trước khi kết thúc) sẽ là trận đấu? – mattytommo