如何从给定的开始日期获取 N 周特定工作日的日期

0 .net c#

我想Wednesdays使用 C#返回一个 DateTime 对象数组,其中包含给定日期当天或之后的下一个计数。方法如下。

 public DateTime[] GetNextWednesdays(int count, string startDate)
 {
     throw new NotImplementedException();
 }
Run Code Online (Sandbox Code Playgroud)

关于如何在没有结束日期的情况下执行此操作存在问题。

tim*_*mur 5

一个稍短的版本供您考虑:

public DateTime[] GetNextWednesdays(int count, string startDate)
{
    var start = DateTime.Parse(startDate);      
    var wednesday = start.AddDays(DayOfWeek.Wednesday-start.DayOfWeek);
    start = (wednesday > start) ? wednesday : wednesday.AddDays(7); 
    return Enumerable.Range(0,count).Select(i => start.AddDays(i*7).Date).ToArray();
}
Run Code Online (Sandbox Code Playgroud)