问题
Assume I have a user interface where the user can select days. Is there a way to check if the days selected are sequential, such as:
4/4, 4/5, 4/6, 4/7, 4/8, 4/9, 4/10 or
4/29, 4/30, 5/1, 5/2, 5/3
I know I probably can loop through the date range and check, but I was more curious if there was a built in method already to check for this.
Regarding the above scenarios, they are in order and they can roll over into the next month.
I am using the .NET Framework 2.0 and can't use LINQ.
Regarding Tom's answer:
DateTime dtStart = new DateTime(2011,5,4);
DateTime dtEnd = new DateTime(2011,5,11);
int numberOfDaysSelected = 7; //Assume 7 days were selected.
TimeSpan ts = dtEnd - dtStart;
if(ts.Days == numberOfDaysSelected - 1)
{
Console.WriteLine("Sequential");
}
else
{
Console.WriteLine("Non-Sequential");
}
回答1:
I do not believe there is a built in method to achieve your desired results but if you can easily tell the earliest and latest dates, you could create a new TimeSpan by subtracting the the earliest date from the latest date and then verifying that the number of days of the timespan matches the number of dates selected - 1.
回答2:
You didn't tell us if the days are ordered.
You didn't tell us if they might fall over a month boundary as in
30, 31, 1.
I'll assume ordered, and I'll assume they won't fall over a month boundary (because your example is ordered, and it doesn't fall over a month boundary).
Then you can say
public bool IsSequential(this IEnumerable<DateTime> sequence) {
Contract.Requires(sequence != null);
var e = sequence.GetEnumerator();
if(!e.MoveNext()) {
// empty sequence is sequential
return true;
}
int previous = e.Current.Date;
while(e.MoveNext()) {
if(e.Current.Date != previous.AddDays(1)) {
return false;
}
previous = e.Current.Date;
}
return true;
}
Note that this solution requires only walking the sequence once. If you don't have an ordered sequence, or if you permit falling over a month boundary the solution is more complicated.
回答3:
Nothing built in but you can build one easily using Linq:
List<DateTime> timeList = new List<DateTime>();
//populate list..
bool isSequential = timeList.Zip(timeList.Skip(1),
(a, b) => b.Date == a.Date.AddDays(1))
.All(x => x);
Edited - misunderstood question first to mean ascending in time as opposed to sequential - fixed that.
回答4:
Extension method using Linq:
public static bool IsContiguous(this IEnumerable<DateTime> dates)
{
var startDate = dates.FirstOrDefault();
if (startDate == null)
return true;
//.All() doesn't provide an indexed overload :(
return dates
.Select((d, i) => new { Date = d, Index = i })
.All(d => (d.Date - startDate).Days == d.Index);
}
Testing it:
List<DateTime> contiguousDates = new List<DateTime>
{
new DateTime(2011, 05, 05),
new DateTime(2011, 05, 06),
new DateTime(2011, 05, 07),
};
List<DateTime> randomDates = new List<DateTime>
{
new DateTime(2011, 05, 05),
new DateTime(2011, 05, 07),
new DateTime(2011, 05, 08),
};
Console.WriteLine(contiguousDates.IsContiguous());
Console.WriteLine(randomDates.IsContiguous());
Returns
True
False
EDIT:
.NET 2-like answer:
public static bool CheckContiguousDates(DateTime[] dates)
{
//assuming not null and count > 0
var startDate = dates[0];
for (int i = 0; i < dates.Length; i++)
{
if ((dates[i] - startDate).Days != i)
return false;
}
return true;
}
回答5:
You can use the TimeGapCalculator of the Time Period Library for .NET to find gaps between multiple time periods (independent of order, count and overlapping):
// ----------------------------------------------------------------------
public void SequentialPeriodsDemo()
{
// sequential
ITimePeriodCollection periods = new TimePeriodCollection();
periods.Add( new Days( new DateTime( 2011, 5, 4 ), 2 ) );
periods.Add( new Days( new DateTime( 2011, 5, 6 ), 3 ) );
Console.WriteLine( "Sequential: " + IsSequential( periods ) );
periods.Add( new Days( new DateTime( 2011, 5, 10 ), 1 ) );
Console.WriteLine( "Sequential: " + IsSequential( periods ) );
} // SequentialPeriodsDemo
// --------------------------------------------------------------------
public bool IsSequential( ITimePeriodCollection periods, ITimePeriod limits = null )
{
return new TimeGapCalculator<TimeRange>(
new TimeCalendar() ).GetGaps( periods, limits ).Count == 0;
} // IsSequential
来源:https://stackoverflow.com/questions/5887079/check-if-date-range-is-sequential-in-c