2012-05-03 16 views
5

Necesito detectar si el día actual es el tercer viernes en el último mes del trimestre.Detectando el tercer viernes en el último mes del trimestre

Para el año 2012 que habría estas cuatro fechas:

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

¿Cuál es una buena manera de hacer esto en C#?

+0

posible duplicado de [Cómo calcular el segundo viernes del mes en C#] (http://stackoverflow.com/questions/6140018/how-to-calculate-2nd-friday-of-month-in-c-sharp) –

+1

posible duplicado de [¿Cómo encontrar el 3er viernes en un mes con C#?] (http://stackoverflow.com/questions/5421972/how-to-find-the-3rd-friday-in-a-month-with-c) – Stefan

+0

Entonces, si la fecha es 2012-06-15 ¿es eso coincidente? ¿O es 2012-05-18 (mes antes del final) que sería el partido? – mattytommo

Respuesta

1

Bueno, puedes empezar con el primer día de ese mes y avanzar hasta encontrar el primer viernes, puesto que se pueden agregar 14 días para llegar a la tercera Viernes

2

O bien, puede ir sin ningún tipo de bucles y simplemente asumir hoy es viernes 3 y encontrar qué día era hace 2 semanas (debe ser viernes del mismo mes del inicio del partido positivo):

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

Puede haber hasta cinco viernes en un mes. Una forma de verificar es agregar un cheque ahora. AddDays (-21) no es el mismo mes. –

0

Usando el método de extensión escrita por Bert Smith en This Answer Aquí se el método IsThirdFridayInLastMonthOfQuarter que hará exactamente lo que buscas ing para:

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

Para usarlo:

bool isThirdFriday = DateHelper.IsThirdFridayInLastMonthOfQuarter(date); 
0

Puede utilizar el 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 

Ahora usted haga su cheque:

// ---------------------------------------------------------------------- 
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; 
} 
Cuestiones relacionadas