for (int i = 1; i <= Totaldays; i++)
{
var d = new DateTime(dtime1.Year, dtime1.Month, i);
if (d.DayOfWeek == DayOfWeek.Monday)
{
Response.Write(d.ToString("dd/MM/yyyy")+"</br>");
}
}
我需要使用循环概念而不是DayOfWeek方法。
答案 0 :(得分:0)
如果你想获得当前mounth的所有星期一,你可以使用这种方法:
public static IEnumerable<DateTime> GetThisMonthMondaysTimes()
{
int daysToMonday = (int) DateTime.Today.DayOfWeek - 1;
DateTime nearestMonday = DateTime.Today.AddDays(daysToMonday);
for (DateTime day = nearestMonday; day.Month == nearestMonday.Month; day = day.AddDays(7))
yield return day;
}