2016-05-20 16 views
-1

内にある連続した日付の範囲を検索...私たちはに特定の日付は、日付範囲のリストを考えると

2016年6月2日、2016年6月3日、2016年6月4日empTimeOffPeriodsと呼ぶことにします私は特定の日付(empRequestedOffDate)は

内に属する連続日付範囲見つける必要
2016年6月8日、2016年6月9日、2016年6月10日、2016年6月11日

だから、

6/4/2016は、6/2/2016-6/4/2016の範囲に該当します。

6/9/2016は、6/8/2016-6/11/2016の範囲に該当します。 。等。

私のempTimeOffPeriodsisはすでにソートされています。

私はVB.net

'Find all approved future events for team employee 
empPtoDates = EventsManager.GetEventPaidTimeOffList(empDTO.UserId).FindAll(Function(x) x.EventDate >= DateTime.Today And x.Status = 1) 

empOverLappingDates = empPtoDates.**'NOT SURE WHAT TO DO HERE** 

'Build "EventType: (PeriodStart-PeriodEnd)" 
If empPtoDates.Count > 0 Then 
stbEventRanges.Append(empEvent).Append(": ") 
           stbEventRanges.Append(empOverLappingDates.First.EventDate.ToShortDateString()).Append("-") 
           stbEventRanges.Append(empOverLappingDates.Last.EventDate.ToShortDateString()) 
End If 
+0

ですか? 'empTimeOffPeriods'はどのように見えますか?元の投稿は実際の範囲ではなく日付のリストでした – Plutonix

+0

私は何をしようとしているのかと思うのは、範囲が日付の範囲内にあるかどうかを調べる前です。 empTimeOffPeriodsは、承認されたタイムオフ日付を示す単一のeventDateを持つクラスです。 –

+0

しかし、1つの日付では、範囲の終わりを示す暗黙の期間(StartDate + 3日)がない限り範囲を定義しません。 1つの日付では範囲が決まりません。質問が日付のリスト(元の投稿のような)から範囲を定義する*連続する日付を検出する方法である場合は、その質問を編集してその質問をする必要があります。 – Plutonix

答えて

0

でこれをやっているが、ここに私の解決策は、範囲が保存されているどのように

Public Function FindDateRanges(ByRef listOfDates As List(Of DateTime)) As List(Of DefinedDateRange) 

     'Find approved date ranges 
     Dim DateRange = New DefinedDateRange(Nothing) 
     Dim DefDateRanges As New List(Of DefinedDateRange) 
     If listOfDates.Count > 0 Then 
      DateRange = New DefinedDateRange(listOfDates(0), listOfDates(0)) 'First start/end date 
      If listOfDates.Count > 1 Then 'Only one time off date in list 

       For index As Integer = 1 To listOfDates.Count - 1 
        If listOfDates(index) = listOfDates(index - 1).AddDays(1) Then 
         DateRange.dtEnd = listOfDates(index) 
        Else 
         DefDateRanges.Add(DateRange) 
         DateRange = New DefinedDateRange(listOfDates(index), listOfDates(index)) 'Next Start/end date 
        End If 
       Next 
       DefDateRanges.Add(DateRange) 

      Else 
       DefDateRanges.Add(DateRange) 
      End If 
     End If 
     Return DefDateRanges 

    End Function 

    Class DefinedDateRange 
     Public dtStart As DateTime, dtEnd As DateTime 

     Public Sub New(dateStart As DateTime, Optional dateEnd As DateTime = Nothing) 
      Me.dtStart = dateStart 
      Me.dtEnd = dateEnd 
     End Sub 
    End Class 
関連する問題