-1

Given a list of date ranges...We'll call empTimeOffPeriods

6/2/2016, 6/3/2016, 6/4/2016
6/8/2016, 6/9/2016, 6/10/2016, 6/11/2016

I need to find which consecutive date range a specific date (empRequestedOffDate) falls within

So,

6/4/2016 would fall withing the 6/2/2016-6/4/2016 Range

6/9/2016 would fall withing the 6/8/2016-6/11/2016 Range...etc.

my empTimeOffPeriodsis already sorted.

I'm doing this in 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
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Richard Fox
  • 11
  • 1
  • 5
  • How are the ranges stored? What does `empTimeOffPeriods` look like? The original post was not really ranges but a list of dates – Ňɏssa Pøngjǣrdenlarp May 20 '16 at 13:37
  • I guess what I am trying to do then is get the ranges before trying to see which range the date falls within. empTimeOffPeriods is a class with a single eventDate indicating an approved time off date. – Richard Fox May 20 '16 at 13:43
  • But a single date doesnt define a range unless there is some implied duration to mark the end of the range (ie StartDate + 3 Days). A single date cant define a range. If the question is how to detect consecutive dates to *define* a range from a list of dates (as in the original post), you should edit the question to ask that. – Ňɏssa Pøngjǣrdenlarp May 20 '16 at 13:45
  • I'm not trying to define a range from a single date, I'm trying to find out which date range within a given list of date ranges a specific date falls within. – Richard Fox May 20 '16 at 13:52
  • `empTimeOffPeriods is a class with a single eventDate` means it doesnt/cant define a range. The fact that a person can look at the list and see ranges doesnt do the program much good. So, is the question how to detect/define ranges from a list of dates? – Ňɏssa Pøngjǣrdenlarp May 20 '16 at 13:59
  • "is the question how to detect/define ranges from a list of date" Is likely my first step, then I need to know which of those ranges a specific date falls within. – Richard Fox May 20 '16 at 14:05
  • So, basically, I need to identify date ranges within a list of sorted dates. – Richard Fox May 20 '16 at 17:26

1 Answers1

0

So, here's my solution

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
Richard Fox
  • 11
  • 1
  • 5