[英]Check if date range is sequential in c#?
假設我有一個用戶界面,用戶可以在其中 select 天。 有沒有辦法檢查所選日期是否是連續的,例如:
4/4、4/5、4/6、4/7、4/8、4/9、4/10 或
4/29、4/30、5/1、5/2、5/3
我知道我可能可以遍歷日期范圍並檢查,但我更好奇是否已經有內置方法來檢查這個。
關於上述情況,它們是有序的,它們可以滾動到下個月。
我正在使用 .NET 框架 2.0 並且不能使用 LINQ。
關於湯姆的回答:
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");
}
我不相信有一種內置方法可以實現您想要的結果,但如果您可以輕松分辨最早和最晚的日期,您可以通過從最晚的日期中減去最早的日期來創建一個新的 TimeSpan,然后驗證時間跨度的天數與選擇的日期數相匹配 - 1。
你沒有告訴我們這些日子是否有安排。
你沒有告訴我們它們是否會超過一個月的界限,如
30, 31, 1.
我假設是有序的,並且我假設它們不會超過一個月的邊界(因為你的示例是有序的,並且它不會超過一個月的邊界)。
然后你可以說
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;
}
請注意,此解決方案只需要遍歷序列一次。 如果您沒有有序序列,或者如果您允許超過一個月的邊界,則解決方案會更加復雜。
沒有內置任何東西,但您可以使用 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);
已編輯-首先將問題誤解為按時間遞增而不是按順序遞增-修復了該問題。
使用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);
}
測試它:
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());
退貨
True
False
編輯:
.NET 2-like 答案:
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;
}
您可以使用.NET的時間段庫的 TimeGapCalculator來查找多個時間段之間的間隙(與順序、計數和重疊無關):
// ----------------------------------------------------------------------
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.