Grouping Months of a particular Time span together using DateTime.

Posted by Calibre2010 on Stack Overflow See other posts from Stack Overflow or by Calibre2010
Published on 2010-04-22T11:42:28Z Indexed on 2010/04/25 10:43 UTC
Read the original article Hit count: 359

Filed under:
|
|
|
|
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

© Stack Overflow or respective owner

Related posts about datetime

Related posts about c#