按月获取一年组中所有周的列表
Get the list of all weeks in a year group by month
我正在开发一个网络表单应用程序,我需要列出一年中的所有星期,条件如下:
一周从星期一开始到星期日结束。
按月分组
降序排列(较新的一天在最上面)直到今天(它应该显示包括今天在内的周间隔)
每个列出的周间隔都是 link 到新页面 - detail.aspx
(我需要捕获
并将开始和结束日期传递给 link 以便我可以检索它
使用查询字符串的新页面)
这是我要存档的输出,例如:从 2015 年初(从 2015 年 1 月 5 日星期一开始)到今天 7 月 29 日应该是这样的, 2015年。我只写了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)
5月到1月应该是一样的...
这就是我现在拥有的:
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);
}
它输出这个:
如何修复我的循环以反转它,以便较新的日期位于顶部,并且 group/separate 按月列出
你反转循环:
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
我正在开发一个网络表单应用程序,我需要列出一年中的所有星期,条件如下:
一周从星期一开始到星期日结束。
按月分组
降序排列(较新的一天在最上面)直到今天(它应该显示包括今天在内的周间隔)
每个列出的周间隔都是 link 到新页面 -
detail.aspx
(我需要捕获 并将开始和结束日期传递给 link 以便我可以检索它 使用查询字符串的新页面)
这是我要存档的输出,例如:从 2015 年初(从 2015 年 1 月 5 日星期一开始)到今天 7 月 29 日应该是这样的, 2015年。我只写了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)
5月到1月应该是一样的...
这就是我现在拥有的:
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);
}
它输出这个:
如何修复我的循环以反转它,以便较新的日期位于顶部,并且 group/separate 按月列出
你反转循环:
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