这题与《列出本月所有星期天的日期》有点相似,稍微变动一下,即可完成。
public IEnumerable<DateTime> AllSundaysInYear(int year)
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
for (int m = 1; m <= 12; m++)
{
int days = ci.Calendar.GetDaysInMonth(year, m);
for (int i = 1; i <= days; i++)
{
if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Sunday)
yield return new DateTime(year, m, i);
}
}
}
{
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
for (int m = 1; m <= 12; m++)
{
int days = ci.Calendar.GetDaysInMonth(year, m);
for (int i = 1; i <= days; i++)
{
if (new DateTime(year, m, i).DayOfWeek == DayOfWeek.Sunday)
yield return new DateTime(year, m, i);
}
}
}