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

后端 未结 6 852
[愿得一人]
[愿得一人] 2020-12-07 02:03

I have a function that returns me only the fridays from a range of dates

public static List GetDates(DateTime startDate, int weeks)
{
    int         


        
相关标签:
6条回答
  • 2020-12-07 02:31

    Just a small improvement on Sarath's answer, for those (like me) who step into this question

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 02:33

    Based on DeBorges answer, here is an extension to get any specific Day

    public static DateTime GetLastSpecificDayOfTheMonth(this DateTime date, DayOfWeek dayofweek)
        {
            var lastDayOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
    
            while (lastDayOfMonth.DayOfWeek != dayofweek)
                lastDayOfMonth = lastDayOfMonth.AddDays(-1);
    
            return lastDayOfMonth;
        }
    
    0 讨论(0)
  • 2020-12-07 02:34

    Call the below function by sending the date as parameter, in which it extracts the month and year from the date parameter and returns the last Friday of that month

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 02:43

    Here's an extension method we are using.

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

    Below is the MbUnit test case

    [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);
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-07 02:45

    You already have the list of Fridays in the given range. Now just query this again like this:

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-07 02:50

    Check what day of the week the first day of the next month is on, then subtract enough days to get a Friday.

    Or, if you already have a list of Fridays, return only those for which adding 7 days gives a date in the next month.

    0 讨论(0)
提交回复
热议问题