Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Scott Waletzko's Blog
Calculating Number of Days from a Series of Dates
Published: 4/15/2009
XMl / RSS

As so often happens, a lot of code I write is in response to a question posted over at the VBCity.com forums. This is one such code snippet - the question was how to calculate the total span of time from the earliest time to the latest time given a set of dates. Enjoy!

VB:
Public Class DateRange 
    Private _fromDate As DateTime 
    Private _toDate As DateTime 
    Private _timeSpan As TimeSpan 
    Public Sub New(ByVal FromDate As DateTime, ByVal ToDate As DateTime) 
        ' validate... 
        If (FromDate.CompareTo(ToDate) <>-1) Then 
            Throw New System.Exception("The FromDate must be earlier than the ToDate.") 
        End If 
        ' store.. 
        _fromDate = FromDate 
        _toDate = ToDate 
        _timeSpan = ToDate.Subtract(FromDate) 
    End Sub 
    Public ReadOnly Property FromDate() As DateTime 
        Get 
            Return _fromDate 
        End Get 
    End Property 
    Public ReadOnly Property ToDate() As DateTime 
        Get 
            Return _toDate 
        End Get 
    End Property 
    Public ReadOnly Property TimeSpan() 
        Get 
            Return _timeSpan 
        End Get 
    End Property 
    Public Function IsInRange(ByVal Value As DateTime) As Boolean 
        Return Value.CompareTo(Me.FromDate) > -1 And Value.CompareTo(Me.ToDate) < 1 
    End Function 
    Public Function IntersectsWith(ByVal Value As DateRange) As Boolean 
        If Me.IsInRange(Value.FromDate) Or Me.IsInRange(Value.ToDate) Then Return True 
    End Function 
    Public Sub CombineWith(ByVal Value As DateRange) 
        ' validate... 
        If Not (Me.IntersectsWith(Value)) Then 
            Throw New System.Exception("The specified date ranges cannot be combined - they do not overlap.") 
        End If 
        ' get full range... 
        If (Me.FromDate.CompareTo(Value.FromDate) = 1) Then 
            _fromDate = Value.FromDate 
        End If 
        If (Me.ToDate.CompareTo(Value.ToDate) = -1) Then 
            _toDate = Value.ToDate 
        End If 
    End Sub 
End Class 
	

Here's how to use it:

VB:
Private Sub Test() 

    Dim dateRanges As New List(Of DateRange) 

    AddDateRange(dateRanges, New Date(2008, 1, 1), New Date(2008, 1, 15)) 
    AddDateRange(dateRanges, New Date(2008, 1, 10), New Date(2008, 1, 25)) 
    AddDateRange(dateRanges, New Date(2008, 1, 1), New Date(2008, 1, 15)) 

    For Each range As DateRange In dateRanges 
        Debug.WriteLine(range.ToString()) 
    Next 

End Sub 

Private Sub AddDateRange(ByVal Ranges As List(Of DateRange), ByVal FromDate As DateTime, ByVal ToDate As DateTime) 
    'create new date range... 
    Dim combined As Boolean = False 
    Dim range As New DateRange(FromDate, ToDate) 
    For Each item As DateRange In Ranges 
        If item.IntersectsWith(range) Then 
            item.CombineWith(range) 
            combined = True 
            Exit For 
        End If 
    Next 
    If Not combined Then Ranges.Add(range) 
End Sub 	
	

...and the output:

1/1/2008 12:00:00 AM to 1/25/2008 12:00:00 AM



Questions or Comments? .

VB to C# and C# to VB translation provided by Instant C# and Instant VB.