Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:52:16+00:00 2026-05-14T07:52:16+00:00

public static string TimeLine2(this HtmlHelper helper, string myString2) { StringBuilder myString3 = new StringBuilder();

  • 0
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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:52:17+00:00Added an answer on May 14, 2026 at 7:52 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 435k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Solutions are simply containers for one or more projects; which… May 15, 2026 at 3:45 pm
  • Editorial Team
    Editorial Team added an answer The asp:Silverlight control is no longer included as of Silverlight… May 15, 2026 at 3:45 pm
  • Editorial Team
    Editorial Team added an answer Not necessarily. You could create multiple threads using the same… May 15, 2026 at 3:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.