How to get the last Friday of the month (s) using .NET

I have a function that returns me only Fridays from a date range

public static List<DateTime> GetDates(DateTime startDate, int weeks)
{
    int days = weeks * 7;

    //Get the whole date range 
    List<DateTime> dtFulldateRange = Enumerable.Range(-days, days).Select(i => startDate.AddDays(i)).ToList();

    //Get only the fridays from the date range
    List<DateTime> dtOnlyFridays = (from dtFridays in dtFulldateRange
                                    where dtFridays.DayOfWeek == DayOfWeek.Friday
                                    select dtFridays).ToList();
    return dtOnlyFridays;
}

Purpose of the function: "A list of dates from the week number indicated before StartDate, that is, if the starting date is April 23, 2010 and the week number is 1, then the program should return the dates from April 16, 2010 to the beginning."

I call the function like:

DateTime StartDate1 = DateTime.ParseExact("20100430", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
List<DateTime> dtList = Utility.GetDates(StartDate1, 4).ToList();

Now the requirement has changed a bit. I need to find out only the last Fridays of each month. Entering the function will remain the same.

+5
source share
5 answers

You already have a list of Fridays in this range. Now just repeat the query as follows:

List<DateTime> lastFridays = (from day in fridays
                              where day.AddDays(7).Month != day.Month
                              select day).ToList<DateTime>();

Hope this helps.

+6

.

public static class DateTimeExtensions
{
    public static DateTime GetLastFridayInMonth(this DateTime date)
    {
        var firstDayOfNextMonth = new DateTime(date.Year, date.Month, 1).AddMonths(1);
        int vector = (((int)firstDayOfNextMonth.DayOfWeek + 1) % 7) + 1;
        return firstDayOfNextMonth.AddDays(-vector);
    }
}

MbUnit

[TestFixture]
public class DateTimeExtensionTests
{
  [Test]
  [Row(1, 2011, "2011-01-28")]
  [Row(2, 2011, "2011-02-25")]
  ...
  [Row(11, 2011, "2011-11-25")]
  [Row(12, 2011, "2011-12-30")]
  [Row(1, 2012, "2012-01-27")]
  [Row(2, 2012, "2012-02-24")]
  ...
  [Row(11, 2012, "2012-11-30")]
  [Row(12, 2012, "2012-12-28")]

  public void Test_GetLastFridayInMonth(int month, int year, string expectedDate)
  {
    var date = new DateTime(year, month, 1);
    var expectedValue = DateTime.Parse(expectedDate);

    while (date.Month == month)
    {
      var result = date.GetLastFridayInMonth();
      Assert.AreEqual(expectedValue, result);
      date = date.AddDays(1);
    }
  }
}
+4

, , , .

, , , 7 .

+1

, ( ),

private DateTime GetLastFridayOfTheMonth(DateTime date)
{
    var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));

    while (lastDayOfMonth.DayOfWeek != DayOfWeek.Friday)
        lastDayOfMonth = lastDayOfMonth.AddDays(-1);

    return lastDayOfMonth;
}
+1

, , Friday

public DateTime GetLastFridayOfMonth(DateTime dt)
{
      DateTime dtMaxValue = DateTime.MaxValue;
      DateTime dtLastDayOfMonth = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month));

      while (dtMaxValue == DateTime.MaxValue)
      {
           // Returns if the decremented day is the fisrt Friday from last(ie our last Friday)
           if (dtMaxValue == DateTime.MaxValue && dtLastDayOfMonth.DayOfWeek == DayOfWeek.Friday)
               return dtLastDayOfMonth;
           // Decrements last day by one
           else
              dtLastDayOfMonth = dtLastDayOfMonth.AddDays(-1.0);
      }
      return dtLastDayOfMonth;
}
-1
source

All Articles