如何循环遍历日期范围?

260

我甚至不确定如何在不使用可怕的for循环/计数器类型解决方案的情况下完成这个问题。 这就是问题所在:

我有两个日期,一个开始日期和一个结束日期,在指定的时间间隔内需要执行某些操作。例如:对于从3/10/2009开始的每一天中的每三天,直到3/26/2009,我都需要在列表中创建一个条目。 因此,我的输入将是:

DateTime StartDate = "3/10/2009";
DateTime EndDate = "3/26/2009";
int DayInterval = 3;
而我的输出将是一个具有以下日期的列表:
3/13/2009 3/16/2009 3/19/2009 3/22/2009 3/25/2009
那么我要怎样做才能实现这个目标呢?我考虑使用一个for循环来遍历范围内的每一天,并使用一个独立的计数器,像这样:
int count = 0;

for(int i = 0; i < n; i++)
{
     count++;
     if(count >= DayInterval)
     {
          //take action
          count = 0;
     }

}

但似乎还有更好的方法?


2
我猜C#有一个日期数据结构可以使用。 - Anna
17个回答

2
您可以使用 DateTime.AddDays() 函数将您的 DayInterval 添加到 StartDate 中,并检查确保它小于 EndDate

1

你可以使用这个。

 DateTime dt0 = new DateTime(2009, 3, 10);
 DateTime dt1 = new DateTime(2009, 3, 26);

 for (; dt0.Date <= dt1.Date; dt0=dt0.AddDays(3))
 {
    //Console.WriteLine(dt0.Date.ToString("yyyy-MM-dd"));
    //take action
 }

真的很简洁。不错! - Paul Mignard

1
你可以考虑编写一个迭代器,这样就可以使用正常的“for”循环语法,如“++”。我搜索并在StackOverflow上找到了一个类似的问题answered,其中提供了关于如何使DateTime可迭代的指针。

0

每15分钟迭代一次

DateTime startDate = DateTime.Parse("2018-06-24 06:00");
        DateTime endDate = DateTime.Parse("2018-06-24 11:45");

        while (startDate.AddMinutes(15) <= endDate)
        {

            Console.WriteLine(startDate.ToString("yyyy-MM-dd HH:mm"));
            startDate = startDate.AddMinutes(15);
        }

0

如果您将日期转换为OADate,您可以像处理任何double数字一样循环遍历它们。

DateTime startDate = new DateTime(2022, 1, 1);
DateTime endDate = new DateTime(2022, 12, 31);

for (double loopDate = startDate.ToOADate(); loopDate <= endDate.ToOADate(); loopDate++)
{
    DateTime selectedDate;
    selectedDate = DateTime.FromOADate(loopDate);
}

0

@jacob-sobus和@mquander和@Yogurt的回答并不完全正确.. 如果我需要第二天,我通常会等到00:00的时间

    public static IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
    {
        for (var day = from.Date; day.Date <= thru.Date; day = day.NextDay())
            yield return day;
    }

    public static IEnumerable<DateTime> EachMonth(DateTime from, DateTime thru)
    {
        for (var month = from.Date; month.Date <= thru.Date || month.Year == thru.Year && month.Month == thru.Month; month = month.NextMonth())
            yield return month;
    }

    public static IEnumerable<DateTime> EachYear(DateTime from, DateTime thru)
    {
        for (var year = from.Date; year.Date <= thru.Date || year.Year == thru.Year; year = year.NextYear())
            yield return year;
    }

    public static DateTime NextDay(this DateTime date)
    {
        return date.AddTicks(TimeSpan.TicksPerDay - date.TimeOfDay.Ticks);
    }

    public static DateTime NextMonth(this DateTime date)
    {
        return date.AddTicks(TimeSpan.TicksPerDay * DateTime.DaysInMonth(date.Year, date.Month) - (date.TimeOfDay.Ticks + TimeSpan.TicksPerDay * (date.Day - 1)));
    }

    public static DateTime NextYear(this DateTime date)
    {
        var yearTicks = (new DateTime(date.Year + 1, 1, 1) - new DateTime(date.Year, 1, 1)).Ticks;
        var ticks = (date - new DateTime(date.Year, 1, 1)).Ticks;
        return date.AddTicks(yearTicks - ticks);
    }

    public static IEnumerable<DateTime> EachDayTo(this DateTime dateFrom, DateTime dateTo)
    {
        return EachDay(dateFrom, dateTo);
    }

    public static IEnumerable<DateTime> EachMonthTo(this DateTime dateFrom, DateTime dateTo)
    {
        return EachMonth(dateFrom, dateTo);
    }

    public static IEnumerable<DateTime> EachYearTo(this DateTime dateFrom, DateTime dateTo)
    {
        return EachYear(dateFrom, dateTo);
    }

0

在这里,你必须小心,不要错过循环中的日期,更好的解决方案是:

使用startdate的第一个日期,并在循环之前将其用于处理所有日期,包括enddate的最后一个日期,因此是<= enddate。

因此,以上答案是正确的。

while (startdate <= enddate)
{
    // do something with the startdate
    startdate = startdate.adddays(interval);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接