【问题标题】:Get the list of all weeks in a year group by month按月获取一年组中所有周的列表
【发布时间】:2015-10-22 01:52:03
【问题描述】:

我正在开发一个网络表单应用程序,我需要列出一年中的所有星期,条件如下:

  • 一周从星期一开始,到星期日结束。

  • 按月分组

  • 按降序(较新的日期在顶部)排序直到今天(它应该显示包括今天日期在内的周间隔)

  • 列出的每个星期间隔都是指向新页面的链接 - detail.aspx(我需要捕获 并将开始和结束日期传递给链接,以便我可以检索它 使用查询字符串的新页面)

这是我要归档的输出,例如:从 2015 年初(从 2015 年 1 月 5 日星期一开始)到今天 2015 年 7 月 29 日,它应该是这样的。我只6 月和 7 月的详细信息,但其余的应该是一样的。

2015 年 7 月

Monday (July, 27) - Sunday (August, 2) <= (each of these is a link)
Monday (July, 20) - Sunday (July, 26)
Monday (July, 13) - Sunday (July, 19)  
Monday (July, 6) - Sunday (July, 12)

2015 年 6 月

Monday (June, 29) - Sunday (July, 5)
Monday (June, 22) - Sunday (June, 28)
Monday (June, 15) - Sunday (June, 21) 
Monday (June, 8) - Sunday (June, 14)
Monday (June, 1) - Sunday (June, 7)    

五月一月应该是一样的...

这就是我现在拥有的:

DateTime counter = new DateTime(2015, 1, 5); // first week of 2015 start on Monday Jan 5 2015
        while (counter <= DateTime.Now)
        {
            DateTime startDate = counter;
            DateTime endDate = startDate.AddDays(6);
            Response.Write("<a href=\"/details.aspx?start=" + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() + "\" target=\"_blank\">" + startDate.ToLongDateString() + " - " + endDate.ToLongDateString() + "<br>");
            counter = endDate.AddDays(1);
        }

它输出这个:

如何修复我的循环以将其反转,以便较新的日期位于顶部,并按月对列表进行分组/分隔

【问题讨论】:

    标签: c# asp.net datetime webforms


    【解决方案1】:

    这里有你自己的代码,我只是添加了几个变量,我觉得它更具可读性

    DateTime counter = new DateTime(2015, 1, 5); // first week of 2015 start on Monday Jan 5 2015
    int currentMonth = counter.Month;
    List<string> rows = new List<string>();
    while (counter <= DateTime.Now)
    {
        DateTime startDate = counter;
        DateTime endDate = startDate.AddDays(6);
        if (!startDate.Month.Equals(currentMonth))
        {   //When the month change it writes the name of the new month
            rows.Add("<br>" + startDate.AddMonths(-1).ToString("MMMM"));
            currentMonth++;
        }
        //I delete your '<br>' at the end to use it in the Join(), also you were missing the closing tag '</a>'
        rows.Add("<a href='/details.aspx?start=" + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() + "' target='_blank'>" + startDate.ToLongDateString() + " - " + endDate.ToLongDateString() + "</a>");
        counter = endDate.AddDays(1);
    }
    rows.Reverse(); //Reverse the order of the array
    Response.Write(string.Join("<br>", rows.ToArray())); // Join the array in one line to just call one time the ResponseWrite
    

    【讨论】:

      【解决方案2】:

      你反转循环:

      int delta = DayOfWeek.Monday - DateTime.Now.DayOfWeek; // finds the difference between today and the monday of this week
      DateTime counter = DateTime.Now.AddDays(delta); // sets the counter the first day of this week.
      
      int currentMonth = DateTime.Now.Month; // current month as integer.
      
      while (counter >= new DateTime(2015, 1, 5))
      {
          if (currentMonth != counter.Month)
          {
              response.Write("my dividing text");// put your html here
              currentMonth = counter.Month;
          }
      
          DateTime startDate = counter;
          DateTime endDate = startDate.AddDays(6);
      
          Response.Write("<a href=\"/details.aspx?start=" 
              + startDate.ToShortDateString() + "&end=" + endDate.ToShortDateString() 
              + "\" target=\"_blank\">" + startDate.ToLongDateString() + " - " 
              + endDate.ToLongDateString() + "<br>");
      
          counter = startDate.AddDays(-7);
       }
      

      【讨论】:

      • 如何通过在每个月初放置一个大月份标题来按月分组/分隔?
      • @RonaldinhoLearnCoding 添加了额外的代码示例。
      • @RonaldinhoLearnCoding 一个错字
      猜你喜欢
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2016-02-12
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-07-27
      相关资源
      最近更新 更多