使用 DateTime 将特定时间跨度的月份分组在一起
public static string TimeLine2(this HtmlHelper helper, string myString2)
{
StringBuilder myString3 = new StringBuilder();
DateTime start = new DateTime(2010, 1, 1);
DateTime end = new DateTime(2011, 12, 12);
myString3.Append("<table>");
myString3.Append("<tr>");
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
DayOfWeek dw = date.DayOfWeek;
var g = date.Month;
var sun = " ";
switch (dw)
{
case DayOfWeek.Sunday:
sun = "S";
break;
case DayOfWeek.Monday:
sun = "M";
break;
case DayOfWeek.Tuesday:
sun = "T";
break;
case DayOfWeek.Wednesday:
sun = "W";
break;
case DayOfWeek.Thursday:
sun = "T";
break;
case DayOfWeek.Friday:
sun = "F";
break;
case DayOfWeek.Saturday:
sun = "S";
break;
}
myString3.Append("<td>" + sun + " " + g + "</td>");
}
myString3.Append("</tr>");
myString3.Append("<tr>");
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
var f = date.Day;
myString3.Append("<td>" + f + "</td>");
}
myString3.Append("</tr>");
myString3.Append("</table>");
return myString3.ToString();
}
基本上,我这里有几个循环,显示一周中的所有天以及一个月中的所有天。这些都放在表格内,所以你会明白
MTWTFSSMT W T F S S M M TWTFSSM 12345678910 11 12 13 14 + + to 31 1234567
我正在尝试编写一种方法,在该方法中我可以分割一周中的所有这些天和几个月中的这些天,以便我的代码返回每个月及其该月中的所有天以及一周中的所有日子,不仅仅是我的时间跨度之间的所有月份,而是将它们分开
MAY MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 12345678 JUNE MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 123456789
public static string TimeLine2(this HtmlHelper helper, string myString2)
{
StringBuilder myString3 = new StringBuilder();
DateTime start = new DateTime(2010, 1, 1);
DateTime end = new DateTime(2011, 12, 12);
myString3.Append("<table>");
myString3.Append("<tr>");
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
DayOfWeek dw = date.DayOfWeek;
var g = date.Month;
var sun = " ";
switch (dw)
{
case DayOfWeek.Sunday:
sun = "S";
break;
case DayOfWeek.Monday:
sun = "M";
break;
case DayOfWeek.Tuesday:
sun = "T";
break;
case DayOfWeek.Wednesday:
sun = "W";
break;
case DayOfWeek.Thursday:
sun = "T";
break;
case DayOfWeek.Friday:
sun = "F";
break;
case DayOfWeek.Saturday:
sun = "S";
break;
}
myString3.Append("<td>" + sun + " " + g + "</td>");
}
myString3.Append("</tr>");
myString3.Append("<tr>");
for (DateTime date = start; date <= end; date = date.AddDays(1))
{
var f = date.Day;
myString3.Append("<td>" + f + "</td>");
}
myString3.Append("</tr>");
myString3.Append("</table>");
return myString3.ToString();
}
Basically, what I have here is a few loops showing all the days of the week and also all the days in a month. This is all placed inside of a table, so you get
MTWTFSSMT W T F S S M M TWTFSSM 12345678910 11 12 13 14 + + to 31 1234567
I'm trying to code a way in which I can split all of these days of the week and days in months so that my code returns each month with all its days in the month and all its days of the week, not just all my months between my timeSpan but splits them so
MAY MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 12345678 JUNE MTWTFSSMTWTFSSMTWTFSSMTWTFSSMTWTF 123456789
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 LINQ:
好的,下一个不使用
LINQ
的变体:您可以根据需要更改输出格式我仅提供了基本示例。
最主要的是迭代必要的日期(使用或不使用 LINQ 是您的选择,但您可能会同意使用 LINQ 的解决方案更优雅)并添加自定义在必要的地方进行格式化(我在第一个示例中添加了注释)。
Using LINQ:
OK, the next variant without
LINQ
:You may change output formatting as you want I provided the basic example only.
The main thing is to iterate through the necessary dates (to use or not to use
LINQ
is you option, but you could agree solution withLINQ
is more elegant) and add custom formatting in the necessary places (I put comments where to do it with the first example).嗨,我不太熟悉 linq,尽管我尝试做另一种方法,但我仍然遇到了一些麻烦。我认为这与我编码 for 循环的方式有关。无论如何,除了使用 linq 之外,还有什么办法可以做到这一点吗?
Hi, i'm not to familiar with linq, though i tried doing it an alternative means i'm still having a little trouble. I think its with how i coded the for loops. Is there anyway to do this otherwise from using linq?