简体   繁体   English

当您使用Calendar控件asp.net c#在日历中选择星期一时,如何显示所有星期一

[英]how to display all the mondays when you select monday in a calendar using Calendar control asp.net c#

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>");
}

I need to display all the Mondays when i select Monday of the current month. 当我选择当月的星期一时,我需要显示所有星期一。 Should i use bool? 我应该使用布尔吗? I have to code using loop concepts. 我必须使用循环概念进行编码。

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>");
}

UPDATE : 更新

All mondays of current month: 本月的所有星期一:

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>");
}

I think the following code should help you in displaying all the Mondays of a particular month. 我认为以下代码应有助于您显示特定月份的所有星期一。

protected void Button1_Click1(object sender, EventArgs e) { 受保护的无效Button1_Click1(对象发送者,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.

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM