简体   繁体   中英

Check number of Dates in Date Range that are sequential in c#?

I'm trying to create a function that returns the number of Dates in a Date Range that are sequential, starting on a specific date.

Example:

StartDate: 9/1/2022

Date Range: 9/1/2022, 9/2/2022, 9/3/2022, 9/4/2022, 9/7/2022

In this scenario the function I'm looking for would return 4.

Assume dates could be unordered and they can roll over into the next month, so with StartDate 9/29/2022:

9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022 would return 3.

I know I can loop through the dates starting at the specific date and check the number of consecutive days, but I'm wondering if there's a clean way to do it with Linq.

This is the cleanest solution I can come up with...

var startDate = new DateTime(2022, 9, 1);

var days = new List<DateTime>()
{
    new(2022, 8, 28),
    new(2022, 9, 1),
    new(2022, 9, 2),
    new(2022, 9, 3),
    new(2022, 9, 4),
    new(2022, 9, 7)
};

var consecutiveDays = GetConsecutiveDays(startDate, days);

foreach (var day in consecutiveDays)
{
    Console.WriteLine(day);
}

Console.ReadKey();

static IEnumerable<DateTime> GetConsecutiveDays(DateTime startDate, IEnumerable<DateTime> days)
{
    var wantedDate = startDate;
    foreach (var day in days.Where(d => d >= startDate).OrderBy(d => d))
    {
        if (day == wantedDate)
        {
            yield return day;
            wantedDate = wantedDate.AddDays(1);
        }
        else
        {
            yield break;
        }
    }
}

Output is: 01.09.2022 0:00:00 02.09.2022 0:00:00 03.09.2022 0:00:00 04.09.2022 0:00:00

If you wanted the count, you can call.Count() on the result or just modify the method... Should be easy.

To count the number of consecutive dates in a given date range.

  1. first parse the dates from a string and order them in ascending order.
  2. Then, use the TakeWhile method to take a sequence of consecutive dates from the start of the list.
  3. Finally, count the number of elements in the returned sequence and display the result.
public class Program
{
    private static void Main(string[] args)
    {
        var dateRange = "9/29/2022, 9/30/2022, 10/1/2022, 10/4/2022";
        var dates = dateRange
            .Split(", ")
            .Select(dateStr =>
            {
                var dateData = dateStr.Split("/");
                var month = int.Parse(dateData[0]);
                var day = int.Parse(dateData[1]);
                var year = int.Parse(dateData[2]);
                return new DateTime(year, month, day);
            })
            .OrderBy(x => x)
            .ToList();

        var consecutiveDatesCounter = dates
            .TakeWhile((date, i) => i == 0 || dates[i - 1].AddDays(1) == date)
            .Count();

        Console.WriteLine(consecutiveDatesCounter);
    }
}

Output: 3

Demo: https://dotnetfiddle.net/tYdWvz

Using a loop would probably be the cleanest way to go. I would use something like the following:

List<DateTime> GetConsecutiveDates(IEnumerable<DateTime> range, DateTime startDate)
{
    var orderedRange = range.OrderBy(d => d).ToList();
    int startDateIndex = orderedRange.IndexOf(startDate);
    if (startDateIndex == -1) return null;

    var consecutiveDates = new List<DateTime> { orderedRange[startDateIndex] };
    for (int i = startDateIndex + 1; i < orderedRange.Count; i++)
    {
        if (orderedRange[i] != orderedRange[i - 1].AddDays(1)) break;

        consecutiveDates.Add(orderedRange[i]);
    }
    return consecutiveDates;
}

Yet another approach using a loop. (I agree with the others that said a loop would be cleaner than using Linq for this task.)

public static int NumConsecutiveDays(IEnumerable<DateTime> dates)
{
    var previous = DateTime.MinValue;
    var oneDay   = TimeSpan.FromDays(1);
    int result   = 0;

    foreach (var current in dates.OrderBy(d => d))
    {
        if (current.Date - previous.Date == oneDay)
            ++result;

        previous = current;
    }

    return result > 0 ? result + 1 : 0;  // Need to add 1 to result if it is not zero.
}
  var dates = new List<DateTime>() {new DateTime(2014,1,1), new DateTime(2014, 1, 2), new DateTime(2014, 1, 3) , new DateTime(2014, 1, 5), new DateTime(2014, 1, 6), new DateTime(2014, 1, 8) };

  var startDate = new DateTime(2014,1,2);

  var EarliestContiguousDates = dates.Where(x => x>=startDate).OrderBy(x => x)
    .Select((x, i) => new { date = x, RangeStartDate = x.AddDays(-i) })
    .TakeWhile(x => x.RangeStartDate == dates.Where(y => y >= startDate).Min()).Count();

You're either going to sort the dates and find sequential ones, or leave it unsorted and repeatedly iterate over the set looking for a match.

Here's the latter dumb approach, leaving it unsorted and using repeated calls to 'IndexOf`:

  public static int CountConsecutiveDays(DateTime startingFrom, List<DateTime> data) 
  {
    int count = 0;
    int index = data.IndexOf(startingFrom);
    while(index != -1) {
      count++;
      startingFrom = startingFrom.AddDays(1);
      index = data.IndexOf(startingFrom);
    }
    return count;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM