2012-05-03 8 views
5

Devo rilevare se il giorno corrente è il terzo venerdì dell'ultimo mese del trimestre.Rilevamento del terzo venerdì nell'ultimo mese del trimestre

Per il 2012 che sarebbe queste quattro date:

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

cosa è un buon modo per fare questo in C#?

+0

possibile duplicato di [Come calcolare 2 ° Venerdì del mese in C#] (http://stackoverflow.com/questions/6140018/how-to-calculate-2nd-friday-of-month-in-c-sharp) –

+1

possibile duplicate di [Come trovare il Venerdì 3 in un mese con C#?] (http://stackoverflow.com/questions/5421972/how-to-find-the-3rd-friday-in-a-month-with-c) – Stefan

+0

Quindi, se la data è 2012-06-15 è che una partita? O è 2012-05-18 (mese prima della fine) che sarebbe la partita? – mattytommo

risposta

1

Ebbene si può iniziare con il primo giorno del mese e avanzare fino a trovare il primo Venerdì, dopo che è possibile aggiungere 14 giorni per arrivare al terzo venerdì

2

In alternativa, si può andare senza alcun loop e semplicemente assumere oggi è 3 ° venerdì e scoprire che giorno era 2 settimane fa (dovrebbe essere venerdì del mese stesso per corrispondenza positiva):

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

Ci possono essere fino a cinque venerdì in un mese. Un modo di controllo è quello di aggiungere un controllo che now.AddDays (-21) non è la stessa mese. –

0

Utilizzando il metodo di estensione scritto da Bert Smith nel This Answer Qui è il metodo IsThirdFridayInLastMonthOfQuarter Che farà esattamente quello che stai cercando ing per:

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; 
    } 
} 

usarlo:

bool isThirdFriday = DateHelper.IsThirdFridayInLastMonthOfQuarter(date); 
0

È possibile utilizzare il 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 

Ora fate il vostro assegno:

// ---------------------------------------------------------------------- 
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; 
} 
Problemi correlati