easy way to loop over months and years from a given date

前端 未结 6 1443
星月不相逢
星月不相逢 2020-12-20 11:53

I have been trying to write this loop and it just keeps getting more complicated. Basically I want to take a given date and loop through every month until I reach the curren

6条回答
  •  Happy的楠姐
    2020-12-20 12:04

    DateTime endDate = DateTime.Today;
    for (DateTime dt = startDate; dt <= endDate; dt = dt.AddMonths(1))
    {
        Console.WriteLine("{0:M/yyyy}", dt);
    }
    

    But if you prefer while loops, then vote for Dan-o. I did. :)

提交回复
热议问题