views:

15

answers:

1

I have a bunch of events with values (how many people went through a doorway). Each event is associated with a Date. There is no fixed period to the events .If no one comes through the door, no event is generated, but often multiple people can go through at once, so the value can be greater than one.

Sometimes events are minutes apart for a time, sometimes there is gaps of a few hours,

Using the Date class, is there an easy way to average my values over periods of time?

A: 

Something like this?

Private Function getAverageDate(ByVal allDates As List(Of Date)) As Date
    If Not allDates Is Nothing AndAlso allDates.Count <> 0 Then
        Dim total As Int64
        For Each d As Date In allDates
            total += d.Ticks
        Next
        Return New Date(System.Convert.ToInt64(total / allDates.Count))
    Else
        Return Date.MinValue
    End If
End Function

or if your looking for the TimeSpan:

Private Function getAveragePeriod(ByVal allPeriods As List(Of TimeSpan)) As TimeSpan
     If Not allPeriods Is Nothing AndAlso allPeriods.Count <> 0 Then
         Dim total As Int64
         For Each ts As TimeSpan In allPeriods
             total += ts.Ticks
         Next
         Return TimeSpan.FromTicks(System.Convert.ToInt64(total / allPeriods.Count))
     Else
         Return TimeSpan.Zero
     End If
End Function
Tim Schmelter