5

我需要检测当前日期是否是本季度最后一个月的第三个星期五

对于 2012 年,这将是这四个日期:

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

在 C# 中执行此操作的好方法是什么?

4

5 回答 5

2

或者,您可以不使用任何循环并简单地假设今天是第 3 个星期五,然后找到 2 周前的哪一天(应该是同一个月的星期五以进行正匹配):

var now = DateTime.UtcNow;
var firstFriday = now.AddDays(-14);
return now.Month % 3 == 0
    && firstFriday.DayOfWeek == DaysOfWeek.Friday
    && now.Month == firstFriday.Month;
于 2012-05-03T07:37:25.353 回答
1

那么你可以从那个月的第一天开始,直到你找到第一个星期五,发布你可以增加 14 天到达第三个星期五

于 2012-05-03T07:27:56.247 回答
0

您可以使用.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

现在您进行检查:

// ----------------------------------------------------------------------
public void CheckDayOfLastQuarterMonth()
{
  DateTime? day = GetDayOfLastQuarterMonth( DayOfWeek.Friday, 3 );
  if ( day.HasValue && day.Equals( DateTime.Now.Date ) )
  {
    // do ...
  }
} // CheckDayOfLastQuarterMonth
于 2012-05-03T11:50:27.453 回答
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;
}
于 2012-05-04T00:38:00.000 回答
0

使用 Bert Smith 在This Answer中编写的扩展方法这是IsThirdFridayInLastMonthOfQuarter完全符合您要求的方法:

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

要使用它:

bool isThirdFriday = DateHelper.IsThirdFridayInLastMonthOfQuarter(date);
于 2012-05-03T09:05:56.470 回答