创建两个日期之间的月份数组

发布于 2024-09-26 20:17:26 字数 436 浏览 0 评论 0原文

我有以下代码片段,用于获取两个日期之间的各个日期:

DateTime[] output = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
    .Select(offset => startDate.AddDays(offset))
    .ToArray(); 

但是,以下部分

endDate.Subtract(startDate).Days

没有 .Months 来返回日期范围内的月份。

例如,如果我提供 1/1/2010 和 6/1/2010,我期望返回 1/1/2010、2/1/2010、3/1/2010、4/1/2010、5/1/ 2010 年和 2010 年 6 月 1 日。

有什么想法吗?

I have the following snippet that I use to get the individual dates between two dates:

DateTime[] output = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
    .Select(offset => startDate.AddDays(offset))
    .ToArray(); 

However, the following section

endDate.Subtract(startDate).Days

does not have a .Months to return the months in the date range.

For example, if I provide 1/1/2010 and 6/1/2010 I would expect to return 1/1/2010, 2/1/2010, 3/1/2010, 4/1/2010, 5/1/2010 and 6/1/2010.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

携君以终年 2024-10-03 20:17:26

试试这个:

static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1)
{
    return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                     .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m));
}

这包括开始月份和结束月份。这会查找有多少个月,然后根据 d0 的年份和月份创建一个新的 DateTime。这意味着月份就像yyyy-MM-01。如果您希望它包含 d0 的时间和日期,您可以将 new DateTime(d0.Year, d0.Month, 1).AddMonths(m) 替换为 >d0.AddMonths(m)

我发现您需要一个数组,在这种情况下,您只需使用 monthsBetween(..., ...).ToArray() 或将 .ToArray() 放入方法内。

Try this:

static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1)
{
    return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                     .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m));
}

This includes both the starting month and the ending month. This finds how many months there is, and then creates a new DateTime based on d0´s year and month. That means the months are like yyyy-MM-01. If you want it to includes the time and day of d0 you can replace new DateTime(d0.Year, d0.Month, 1).AddMonths(m) by d0.AddMonths(m).

I see you need an array, in that case you just use monthsBetween(..., ...).ToArray() or put .ToArray() inside the method.

孤单情人 2024-10-03 20:17:26

因为我只需要两个日期之间的年份和月份,所以我稍微修改了 Lasse Espeholt 的答案。认为:
d0 = 2012-11-03

d1 = 2013-02-05

结果将是这样的:

2012-11

2012-12

2013-01

2013-02

 private List<Tuple<int,int>> year_month_Between(DateTime d0, DateTime d1)
    {
        List<DateTime> datemonth= Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                         .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m)).ToList();
     List<Tuple<int, int>> yearmonth= new List<Tuple<int,int>>();

        foreach (DateTime x in datemonth)
        {
            yearmonth.Add(new Tuple<int, int>(x.Year, x.Month));
        }
        return yearmonth;
    }

Since I just needed the year and month in between two dates I modified Lasse Espeholt answer a little. suppose:
d0 = 2012-11-03

d1 = 2013-02-05

The result will be something like this:

2012-11

2012-12

2013-01

2013-02

 private List<Tuple<int,int>> year_month_Between(DateTime d0, DateTime d1)
    {
        List<DateTime> datemonth= Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1))
                         .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m)).ToList();
     List<Tuple<int, int>> yearmonth= new List<Tuple<int,int>>();

        foreach (DateTime x in datemonth)
        {
            yearmonth.Add(new Tuple<int, int>(x.Year, x.Month));
        }
        return yearmonth;
    }
半世晨晓 2024-10-03 20:17:26

这是您要找的吗?这个要求非常模糊。

DateTime[] calendarMonthBoundaries = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
    .Select(offset => startDate.AddDays(offset))
    .Where(date => date.Day == 1)
    .ToArray();

Is this what you are looking for? The requirement is very ambiguous.

DateTime[] calendarMonthBoundaries = Enumerable.Range(0, 1 + endDate.Subtract(startDate).Days)
    .Select(offset => startDate.AddDays(offset))
    .Where(date => date.Day == 1)
    .ToArray();
大海や 2024-10-03 20:17:26

您可以使用以下方法枚举月份的增量:

private static IEnumerable<DateTime> ByMonths(DateTime startDate, DateTime endDate)
{
  DateTime cur = startDate;

  for(int i = 0; cur <= endDate; cur = startDate.AddMonths(++i))
  {
    yield return cur;
  }
}

然后如果您想要一个数组,则对其调用 ToArray() 。拥有可能是想要的价值观是相当好的。例如,如果您从 1 月 31 日开始,接下来的日期是 2 月 28 日(闰年为 29),然后是 3 月 31 日< super>st,然后是 4 月 30 日,依此类推。

You could enumerate increments of months with:

private static IEnumerable<DateTime> ByMonths(DateTime startDate, DateTime endDate)
{
  DateTime cur = startDate;

  for(int i = 0; cur <= endDate; cur = startDate.AddMonths(++i))
  {
    yield return cur;
  }
}

and then call ToArray() on that if you want an array. It's reasonably good about having values that are likely to be what is wanted; e.g. if you start at Jan 31st you'll next get Feb 28th (or 29th on leap years), then Mar 31st, then Apr 30th and so on.

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