Home > Software design >  find the smallest period in list of dates
find the smallest period in list of dates

Time:05-10

is there a way an idea to find the smallest period in list of dates:

A: startDate|----------------------|EndDate
B: startDate|------------|EndDate
C: startDate|-----------------|EndDate

the smallest period is in this exemple is B. I need an idea to begin

CodePudding user response:

Calculate the difference in time to get a sequence of timeSpans, and pick the smallest one. For example using LINQ

var shortestPeriod = myDates.Min(d => d.startDate - s.EndDate);

If you want the actual start and end dates for the shortest interval you could use MinBy from MoreLinq, or write your own equivalent extension method.

  • Related