Count number of Mondays in a given date range

后端 未结 15 1303
南旧
南旧 2020-11-28 09:35

Given a date range, I need to know how many Mondays (or Tuesdays, Wednesdays, etc) are in that range.

I am currently working in C#.

15条回答
  •  星月不相逢
    2020-11-28 09:50

    I've come across a slightly easier way to solve this problem using linq.

    public static int NumberOfFridays(DateTime start, DateTime end) 
    { 
        return start.GetDaysInBetween(end, inclusive: true).Count(d => d.DayOfWeek == DayOfWeek.Friday); 
    } 
    

    Hope that helps.

提交回复
热议问题