迭代一年中的所有日子

时间:2014-08-01 12:48:58

标签: c# datetime

我希望按以下格式获取特定年份的List<string>天数列表:dd.MM.yyyy

public List<string> GetDays(int year)
{
    List<string> lst = new List<string>();

    foreach-day-in-year // TO DO iterate days from given year
    {
       string day="";

       // TO DO  iterate all days

       lst.Add(day);
    }       

    return lst;
}

1 个答案:

答案 0 :(得分:2)

    public IList<string> GetDays(int year)
    {
        var days = new List<string>();
        var start = new DateTime(year, 1, 1);

        while (start.Year == year)
        {
            days.Add(start.ToString("dd.MM.yyyy"));
            start = start.AddDays(1);
        }

        return days;
    }