protected void Button1_Click1(object sender, EventArgs e)
{
DateTime dtime1 = new DateTime();
dtime1 = Calendar1.SelectedDate;
DateTime dtime = new DateTime();
dtime = dtime1.AddDays(7);
Response.Write(dtime.ToString("dd/MM/yyyy") + "</br>");
}
当我选择当月的星期一时,我需要显示所有星期一。我应该使用bool吗?我必须使用循环概念进行编码。
答案 0 :(得分:2)
DateTime dtime1 = Calendar1.SelectedDate;
int iTotalDays = DateTime.DaysInMonth(dtime1.Year, dtime1.Month) - dtime1.Day;
for (int i = 1; i <= iTotalDays; i++)
{
var d = dtime1.AddDays(i);
if (d.DayOfWeek == DayOfWeek.Monday)
Response.Write(d.ToString("dd/MM/yyyy") + "</br>");
}
<强>更新强>:
本月的所有星期一:
DateTime dtime1 = DateTime.Now;
int iTotalDays = DateTime.DaysInMonth(dtime1.Year, dtime1.Month);
for (int i = 1; i <= iTotalDays; i++)
{
var d = new DateTime(dtime1.Year, dtime1.Month, i);
if (d.DayOfWeek == DayOfWeek.Monday)
Response.Write(d.ToString("dd/MM/yyyy") + "</br>");
}
答案 1 :(得分:0)
我认为以下代码可以帮助您显示特定月份的所有星期一。
protected void Button1_Click1(object sender,EventArgs e) {
DateTime dtime1 = new DateTime();
dtime1 = Calendar1.SelectedDate;
// get a list of all dates
var dates = Enumerable.Range(1, DateTime.DaysInMonth(dtime1.Year, dtime1.Month)).Select(n => new DateTime(dtime1.Year, dtime1.Month, n));
// then filter mondays like following
var mondays = from d in dates
where d.DayOfWeek == DayOfWeek.Monday
select d;
List<DateTime> listOfMondays = mondays.ToList();
// then you can loop through the listOfMondays and write it on the response.
}