逐月获取一年中所有周的列表

时间:2015-07-30 15:42:40

标签: c# asp.net datetime webforms

我正在开发一个webform应用程序,我需要列出一年中的所有周,具备以下条件:

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

  • 逐月分组

  • 按降序排列(最新日期)直到今天(它应显示包括今天日期的周间隔)

  • 每个列出的周间隔都是新页面的链接 - 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)    

可能 Jan 应该是相同的......

这就是我现在所拥有的:

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

输出:

enter image description here

如何修复我的循环以反转它以使较新的日期位于顶部并按月分组/分隔

2 个答案:

答案 0 :(得分:1)

你扭转了循环:

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

答案 1 :(得分: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