[英]Get the list of all weeks in a year group by month
我正在使用Webform應用程序,因此需要列出一年中的所有星期,並滿足以下條件:
一周從星期一開始,到星期日結束。
按月分組
直到今天為止(以降序排列(從上到下))(應該顯示星期幾,包括今天的日期)
列出的每個星期間隔都是指向新頁面的鏈接detail.aspx
(我需要捕獲開始日期和結束日期並將其傳遞給鏈接,以便可以使用查詢字符串為新頁面檢索它)
例如,這是我要存檔的輸出 :這是從2015年初(從2015年1月5日星期一開始)到2015年7月29日今天的樣子。我只寫了Jun和July的詳細信息,其余部分應該是一樣的。
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)
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);
}
它輸出:
我該如何修復循環以使其反向,因此較新的日期在最前面,並且還按月對列表進行分組/分隔
您反轉循環:
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);
}
在這里,您有自己的代碼,我只添加了一些變量,我認為它更具可讀性
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.