在StartDate和EndDate之间每天迭代[重复]

时间:2022-08-25 11:04:55

This question already has an answer here:

这个问题在这里已有答案:

I have a DateTime StartDate and EndDate.

我有一个DateTime StartDate和EndDate。

How can I, irrespective of times, iterate across each Day between those two?

无论何时,我怎样才能在这两者之间每天进行迭代?

Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010 1:59:12 AM.

示例:StartDate是2010年7月20日下午5:10:32,EndDate是2010年7月29日上午1:59:12。

I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.

我希望能够跨越7 / 20,7 / 21,7 / 22 .. 7/29进行迭代。

5 个解决方案

#1


115  

for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
    ...
}

The .Date is to make sure you have that last day, like in the example.

.Date是为了确保你有最后一天,就像在例子中一样。

#2


11  

An alternative method that might be more reusable is to write an extension method on DateTime and return an IEnumerable.

可能更可重用的替代方法是在DateTime上编写扩展方法并返回IEnumerable。

For example, you can define a class:

例如,您可以定义一个类:

public static class MyExtensions
{
    public static IEnumerable EachDay(this DateTime start, DateTime end)
    {
        // Remove time info from start date (we only care about day). 
        DateTime currentDay = new DateTime(start.Year, start.Month, start.Day);
        while (currentDay <= end)
        {
            yield return currentDay;
            currentDay = currentDay.AddDays(1);
        }
    }
}

Now in the calling code you can do the following:

现在在调用代码中,您可以执行以下操作:

DateTime start = DateTime.Now;
DateTime end = start.AddDays(20);
foreach (var day in start.EachDay(end))
{
    ...
}

Another advantage to this approach is that it makes it trivial to add EachWeek, EachMonth etc. These will then all be accessible on DateTime.

这种方法的另一个优点是它使添加EveryWeek,EachMonth等变得微不足道。然后,这些都可以在DateTime*问。

#3


3  

You have to be careful about end-date. For example, in

您必须小心结束日期。例如,在

Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010 1:59:12 AM.
I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.

示例:StartDate是2010年7月20日下午5:10:32,EndDate是7/29/2010 1:59:12 AM。我希望能够跨越7 / 20,7 / 21,7 / 22 .. 7/29进行迭代。

date < endDate will not include 7/29 ever. When you add 1 day to 7/28 5:10 PM - it becomes 7/29 5:10 PM which is higher than 7/29 2 AM.

date 将不包括7>

If that is not what you want then I'd say you do

如果这不是你想要的那么我会说你做的

for (DateTime date = start.Date; date <= end.Date; date += TimeSpan.FromDays(1))
{
     Console.WriteLine(date.ToString());
}

or something to that effect.

或者那种效果。

#4


3  

The loops of @Yuriy Faktorovich, @healsjnr and @mho will all throw a System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime exception if EndDate == DateTime.MaxValue. To prevent this, add an extra check at the end of the loop

@Yuriy Faktorovich,@ hersjnr和@mho的循环都将抛出System.ArgumentOutOfRangeException:如果EndDate == DateTime.MaxValue,则添加或减去的值将导致无法表示的DateTime异常。为防止这种情况,请在循环结束时添加额外的检查

for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
   ...
   if (date.Date == DateTime.MaxValue.Date)
   {
       break;
   }
}

(I would have posted this as a comment to @Yuriy Faktorovich's answer, but I lack reputation)

(我会发布这个评论来评论@Yuriy Faktorovich的答案,但我缺乏声誉)

#5


0  

DateTime date = DateTime.Now;
DateTime endDate = date.AddDays(10);

while (date < endDate)
{
  Console.WriteLine(date);
  date = date.AddDays(1);
}

#1


115  

for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
    ...
}

The .Date is to make sure you have that last day, like in the example.

.Date是为了确保你有最后一天,就像在例子中一样。

#2


11  

An alternative method that might be more reusable is to write an extension method on DateTime and return an IEnumerable.

可能更可重用的替代方法是在DateTime上编写扩展方法并返回IEnumerable。

For example, you can define a class:

例如,您可以定义一个类:

public static class MyExtensions
{
    public static IEnumerable EachDay(this DateTime start, DateTime end)
    {
        // Remove time info from start date (we only care about day). 
        DateTime currentDay = new DateTime(start.Year, start.Month, start.Day);
        while (currentDay <= end)
        {
            yield return currentDay;
            currentDay = currentDay.AddDays(1);
        }
    }
}

Now in the calling code you can do the following:

现在在调用代码中,您可以执行以下操作:

DateTime start = DateTime.Now;
DateTime end = start.AddDays(20);
foreach (var day in start.EachDay(end))
{
    ...
}

Another advantage to this approach is that it makes it trivial to add EachWeek, EachMonth etc. These will then all be accessible on DateTime.

这种方法的另一个优点是它使添加EveryWeek,EachMonth等变得微不足道。然后,这些都可以在DateTime*问。

#3


3  

You have to be careful about end-date. For example, in

您必须小心结束日期。例如,在

Example: StartDate is 7/20/2010 5:10:32 PM and EndDate is 7/29/2010 1:59:12 AM.
I want to be able to iterate across 7/20, 7/21, 7/22 .. 7/29.

示例:StartDate是2010年7月20日下午5:10:32,EndDate是7/29/2010 1:59:12 AM。我希望能够跨越7 / 20,7 / 21,7 / 22 .. 7/29进行迭代。

date < endDate will not include 7/29 ever. When you add 1 day to 7/28 5:10 PM - it becomes 7/29 5:10 PM which is higher than 7/29 2 AM.

date 将不包括7>

If that is not what you want then I'd say you do

如果这不是你想要的那么我会说你做的

for (DateTime date = start.Date; date <= end.Date; date += TimeSpan.FromDays(1))
{
     Console.WriteLine(date.ToString());
}

or something to that effect.

或者那种效果。

#4


3  

The loops of @Yuriy Faktorovich, @healsjnr and @mho will all throw a System.ArgumentOutOfRangeException: The added or subtracted value results in an un-representable DateTime exception if EndDate == DateTime.MaxValue. To prevent this, add an extra check at the end of the loop

@Yuriy Faktorovich,@ hersjnr和@mho的循环都将抛出System.ArgumentOutOfRangeException:如果EndDate == DateTime.MaxValue,则添加或减去的值将导致无法表示的DateTime异常。为防止这种情况,请在循环结束时添加额外的检查

for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
{
   ...
   if (date.Date == DateTime.MaxValue.Date)
   {
       break;
   }
}

(I would have posted this as a comment to @Yuriy Faktorovich's answer, but I lack reputation)

(我会发布这个评论来评论@Yuriy Faktorovich的答案,但我缺乏声誉)

#5


0  

DateTime date = DateTime.Now;
DateTime endDate = date.AddDays(10);

while (date < endDate)
{
  Console.WriteLine(date);
  date = date.AddDays(1);
}