【问题标题】:Get Dates lists between 2 dates C#获取两个日期之间的日期列表 C#
【发布时间】:2018-09-17 20:21:32
【问题描述】:

如何获取两个日期之间的日期范围列表

例如,

start date 是 2 月 12 日 end date 是 5 月 23 日

所以,结果一定是这样的

12-Feb to 28-Feb 1-Mar to 31-Mar 1-April to 30-April 1-May to 23-May

【问题讨论】:

  • 为什么二月变成了二月,而四月却是四月?
  • 您需要遍历这些范围还是只需要一个包含所需日期的列表?
  • 基本上你为第一天创建 DateTime 并添加一天直到最后一天。使用 AddDays。谷歌代码片段。
  • 循环范围

标签: c# asp.net


【解决方案1】:

假设您只检查一年,即当前一年(2018 年)。 当然你可以将StartDateEndDate参数化。

下面的代码没有什么特别之处;我使用"d-MMMM" 作为日期格式,因为您似乎要求最少天数("d" 这样做,即在一个月的第一天选择1 而不是01)和“长”名称本月(“MMMM”这样做,检查this answer)。 如果您只需要缩写,即“Feb”代表“February”,则可以使用“MMM”(检查this other answer)。

DateTime StartDate = new DateTime(2018, 2, 12);
DateTime EndDate = new DateTime(2018, 5, 23);

for (int i = 0; i <= EndDate.Month - StartDate.Month; i++)
{
    DateTime FirstDay = new DateTime(2018, StartDate.Month + i, 1);
    if (i == 0)
    {
        FirstDay = StartDate;
    }
    DateTime LastDay = new DateTime(2018, StartDate.Month + i, DateTime.DaysInMonth(2018, FirstDay.Month));
    if (i == EndDate.Month - StartDate.Month)
    {
        LastDay = EndDate;
    }
    Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
}

Console.ReadLine();

您也可以使用以下代码使其“跨年”:

static void Main(string[] args)
{
    PrintDateList(new DateTime(2018, 2, 12), new DateTime(2018, 5, 23));
    PrintDateList(new DateTime(2018, 11, 12), new DateTime(2019, 2, 23));

    Console.ReadLine();
}

private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
    Console.WriteLine("");
    int TotalMonths = EndDate.Month - StartDate.Month +
                     (EndDate.Year - StartDate.Year) * 12;

    int CurrentYear = StartDate.Year;
    int MonthsToSubtract = 0;

    for (int i = 0; i <= TotalMonths; i++)
    {
        int CurrentMonth = StartDate.Month + i;

        if (StartDate.Month + i > 12)
        {
            if ((StartDate.Month + i) % 12 == 1)
            {
                CurrentYear++;
                Console.WriteLine(CurrentYear.ToString());
                MonthsToSubtract = StartDate.Month + i - 1;
            }

            CurrentMonth = StartDate.Month + i - MonthsToSubtract;
        }

        DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
        if (i == 0)
        {
            FirstDay = StartDate;
        }
        DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
        if (i == TotalMonths)
        {
            LastDay = EndDate;
        }
        Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
    }
}

输出是

2 月 12 日至 2 月 28 日
3 月 1 日至 3 月 31 日
4 月 1 日至 4 月 30 日
5 月 1 日至 5 月 23 日

11 月 12 日至 11 月 30 日
12 月 1 日至 12 月 31 日
1 月 1 日至 1 月 31 日
2 月 1 日至 2 月 23 日

既然你问了,我就把Console.WriteLine换成了List

private static void PrintDateList(DateTime StartDate, DateTime EndDate)
{
        List<Tuple<DateTime>> EventsDatesRangeList = new List<Tuple<DateTime>>();
        Console.WriteLine("");
        int TotalMonths = EndDate.Month - StartDate.Month +
                        (EndDate.Year - StartDate.Year) * 12;

        int CurrentYear = StartDate.Year;
        int MonthsToSubtract = 0;

        for (int i = 0; i <= TotalMonths; i++)
        {
            int CurrentMonth = StartDate.Month + i;

            if (StartDate.Month + i > 12)
            {
                if ((StartDate.Month + i) % 12 == 1)
                {
                    CurrentYear++;
                    Console.WriteLine(CurrentYear.ToString());
                    MonthsToSubtract = StartDate.Month + i - 1;
                }

                CurrentMonth = StartDate.Month + i - MonthsToSubtract;
            }

            DateTime FirstDay = new DateTime(CurrentYear, CurrentMonth, 1);
            if (i == 0)
            {
                FirstDay = StartDate;
            }
            DateTime LastDay = new DateTime(CurrentYear, CurrentMonth, DateTime.DaysInMonth(CurrentYear, FirstDay.Month));
            if (i == TotalMonths)
            {
                LastDay = EndDate;
            }
                Console.WriteLine(FirstDay.ToString("d-MMMM") + " to " + LastDay.ToString("d-MMMM"));
            EventsDatesRangeList.Add(new Tuple<DateTime>(FirstDay));
            EventsDatesRangeList.Add(new Tuple<DateTime>(LastDay));

        }
    }

【讨论】:

  • 非常好,你能试试这个日期吗(新日期时间(2018、2、12)、新日期时间(2019、11、12)
  • 我猜你在问,因为它给了 2019 年 2 月的 29 天,对吧?我编辑了我的代码并在 2019 年和 2020 年(闰年)进行了测试。
  • 查看 insted 我打印结果并将其存储在创建的列表中
  • EventsDatesRangeList.Add(new Tuple(FirstDay)); EventsDatesRangeList.Add(new Tuple(LastDay));
  • 替换代码然后查看结果代码最终将年份改为2020
猜你喜欢
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 2017-06-12
相关资源
最近更新 更多