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

发布于 2024-08-30 13:04:46 字数 2118 浏览 10 评论 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();
}

基本上,我这里有几个循环,显示一周中的所有天以及一个月中的所有天。这些都放在表格内,所以你会明白

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-09-06 13:04:46

使用 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 的解决方案更优雅)并添加自定义在必要的地方进行格式化(我在第一个示例中添加了注释)。

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

茶底世界 2024-09-06 13:04:46
 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 之外,还有什么办法可以做到这一点吗?

 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文