如何使用日历中的循环概念检查所选日期是否为星期一?

时间:2014-06-30 10:57:19

标签: c# asp.net

 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方法。

1 个答案:

答案 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;
    }