2012-05-03 41 views
5

Tôi cần phát hiện nếu ngày hiện tại là thứ sáu thứ sáu trong tháng cuối cùng của quý.Phát hiện ngày Thứ Sáu thứ ba trong tháng cuối cùng của quý

Đối với năm 2012 đó sẽ là bốn ngày:

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

Cách tốt để thực hiện điều này trong C# là gì?

+0

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) –

+1

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

+0

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

Trả lời

1

Vâng, bạn có thể bắt đầu với ngày đầu tiên của tháng đó và trước cho đến khi bạn tìm thấy thứ sáu đầu tiên, bài mà bạn có thể thêm 14 ngày để đến nơi vào thứ Sáu thứ ba

2

Hoặc, bạn có thể đi mà không có bất kỳ vòng lặp và chỉ cần giả hôm nay là thứ 3 thứ Sáu và tìm thấy những gì ngày đó là 2 tuần trước (nên thứ sáu của tháng cùng cho trận đấu dương):

var now = DateTime.UtcNow; 
var firstFriday = now.AddDays(-14); 
return now.Month % 3 == 0 
    && firstFriday.DayOfWeek == DaysOfWeek.Friday 
    && now.Month == firstFriday.Month; 
+1

Có thể có đến năm thứ sáu trong một tháng. Một cách kiểm tra là thêm một kiểm tra mà now.AddDays (-21) không phải là cùng một tháng. –

0

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); 
0

Bạn có thể sử dụng 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 

Bây giờ bạn làm séc:

// ---------------------------------------------------------------------- 
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; 
} 
Các vấn đề liên quan