views:

195

answers:

2
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
+1  A: 

Using 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
            });
    });

OK, the next variant without 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();

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 with LINQ is more elegant) and add custom formatting in the necessary places (I put comments where to do it with the first example).

Alex
Thanks Man, appreciate it, do i have to do another loop to get more than one year.?
Calibre2010
@Calibre2010: nope, another loop is not required here. The only thing you have to do is to change the start and/or endDate variable.
Alex
If i add a variable to the year value it errors out as it cannot see an increase in the year, just increments in the monthly value?
Calibre2010
@Calibre2010: if you want to do this for e.g. 2 years just change the second line of my example to `DateTime endDate = new DateTime(2011, 12, 31);`. Nothing else is required.
Alex
I get this error after typing ---- that Year, Month, and Day parameters describe an un-representable DateTime Is it the year is not incrememnting in.. DateTime d1 = new DateTime(startDate.Year, startDate.Month + i, 1);
Calibre2010
yep, man you're right. There was a bug. The fix near the first line: `DateTime d1 = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(i);`. Hope, this helps.
Alex
Cheers, works a charm :) appreciate it.
Calibre2010
A: 
 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();

    }

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?

Calibre2010
@Calibre2010: and please mark question as answered if you feel the answer is correct or provide an additional info if something is going wrong.
Alex