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

后端 未结 6 863
[愿得一人]
[愿得一人] 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: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;
        }
    

提交回复
热议问题