soutsjjw
8/7/2019 - 5:32 AM

foreach DateTime

日期遍歷

void Main()
{
    DateTime StartDate = DateTime.Parse("2019/01/01");
    DateTime EndDate = DateTime.Parse("2019/12/31");

    foreach (DateTime day in EachDay(StartDate, EndDate))
    {
        Console.WriteLine(day);
    }
}

public IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
{
    for(var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
        yield return day;
}