使用DateTime将特定时间跨度的月份分组

时间:2010-04-22 11:42:29

标签: c# datetime loops dayofweek

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

2 个答案:

答案 0 :(得分:1)

使用LINQ:

DateTime startDate = new DateTime(2010, 1, 1);
DateTime endDate = new DateTime(2010, 12, 31);

int monthCount =
    (endDate.Month - startDate.Month + 1) +
    (endDate.Year - startDate.Year) * 12;

Enumerable
    .Range(0, monthCount)
    .Select(x => new DateTime(startDate.Year, startDate.Month, 1).AddMonths(x))
    .ToList()
    .ForEach(d1 =>
    {
        string month = d1.ToString("MMMM");
        // here should be your code
        // to work with months

        Enumerable
            .Range(0, d1.AddMonths(1).AddDays(-1).Day)
            .Select(x => d1.AddDays(x))
            .ToList()
            .ForEach(d2 =>
            {
                string dayOfWeek = d2.ToString("ddd");
                string day = d2.Day.ToString();
                // here should be your code
                // to work with days
            });
    });

好的,没有LINQ的下一个变体:

StringBuilder sb = new StringBuilder();

DateTime startDate = new DateTime(2010, 1, 1);
DateTime endDate = new DateTime(2012, 12, 31);

int monthCount =
    (endDate.Month - startDate.Month + 1) +
    (endDate.Year - startDate.Year) * 12;

for (int i = 0; i < monthCount; i++)
{
    DateTime d1 = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(i);
    string month = d1.ToString("MMMM");

    sb.AppendFormat("<p>{0}</p>", month);

    int daysInMonth = d1.AddMonths(1).AddDays(-1).Day;
    StringBuilder daysOfWeekRow = new StringBuilder();
    StringBuilder daysRow = new StringBuilder();
    for (int j = 0; j < daysInMonth; j++)
    {
        DateTime d2 = d1.AddDays(j);
        string dayOfWeek = d2.ToString("ddd");
        string day = d2.Day.ToString();

        daysOfWeekRow.AppendFormat("<td>{0}</td>", dayOfWeek);
        daysRow.AppendFormat("<td>{0}</td>", day);
    }
    sb.AppendFormat(
        "<table><tr>{0}</tr><tr>{1}</tr></table>",
        daysOfWeekRow.ToString(), 
        daysRow.ToString()
    );
}

string result = sb.ToString();

您可以根据需要更改输出格式我只提供基本示例。

主要是迭代必要的日期(使用或不使用LINQ是你的选择,但你可以同意LINQ更优雅的解决方案)并在其中添加自定义格式必要的地方(我把评论放在第一个例子的地方)。

答案 1 :(得分:1)

 public static string TimeLine(this HtmlHelper helper, string myString1)
    {
        StringBuilder string2 = new StringBuilder();

        DateTime startDate = new DateTime(2010, 1, 1);
        DateTime endDate = new DateTime(2011, 12, 12);

        string2.Append("<table>");

        for (DateTime date = startDate; date <= endDate; date = date.AddMonths(1))
        {



            string2.Append("<p>" + date.AddMonths(1) + "</p>");

            //DateTime ddd = new DateTime(year, month);

            string2.Append("<tr>");

            for (date = startDate; date <= endDate; date = date.AddMonths(1).AddDays(1))
            {
                DayOfWeek dw = date.DayOfWeek;

                var dateShortHand = "";

                switch (dw)
                {
                    case DayOfWeek.Monday:
                        dateShortHand = "M";
                        break;
                    case DayOfWeek.Tuesday:
                        dateShortHand = "T";
                        break;
                    case DayOfWeek.Wednesday:
                        dateShortHand = "W";
                        break;
                    case DayOfWeek.Thursday:
                        dateShortHand = "T";
                        break;
                    case DayOfWeek.Friday:
                        dateShortHand = "F";
                        break;
                    case DayOfWeek.Saturday:
                        dateShortHand = "S";
                        break;
                    case DayOfWeek.Sunday:
                        dateShortHand = "S";
                        break;
                }
                string2.Append("<td>" + dateShortHand + "</td>");

            }
            string2.Append("</tr>");





            string2.Append("<tr>");

            //for (int i = 1; i <= ff; date = date.AddDays(1))
            //{

            //    var f = date.Day;
            //    string2.Append("<td>" + f + "</td>");

            //}
            string2.Append("</tr>");



        }



        string2.Append("</table>");


        return string2.ToString();

    }

嗨,我不熟悉linq,虽然我尝试了另外一种方法,但我仍然有点麻烦。我认为它与我如何编码for循环。无论如何,使用linq还是要这样做吗?