C# - 開始曜日を指定して、月の週のリストを取得する最適な方法は何ですか? 質問する

C# - 開始曜日を指定して、月の週のリストを取得する最適な方法は何ですか? 質問する

月曜日を開始日として、特定の月の週のリストを取得する必要があります。

たとえば、2009 年 2 月の場合、このメソッドは次を返します。

2/2/2009
2/9/2009
2/16/2009
2/23/2009

ベストアンサー1

 // Get the weeks in a month

 DateTime date = DateTime.Today;
 // first generate all dates in the month of 'date'
 var dates = Enumerable.Range(1, DateTime.DaysInMonth(date.Year, date.Month)).Select(n => new DateTime(date.Year, date.Month, n));
 // then filter the only the start of weeks
 var weekends = from d in dates
                where d.DayOfWeek == DayOfWeek.Monday
                select d;

おすすめ記事