DateTime - Get Average Time from multiple times

  • Thread starter Thread starter sho_nuff
  • Start date Start date
S

sho_nuff

Hello all,

I have several DateTime objects and i want to get the average of all of them.

What would be the easiest way to do this?

Thanks
SN
 
sho_nuff said:
I have several DateTime objects and i want to
get the average of all of them.
What would be the easiest way to do this?

private static DateTime AverageDateTime(params DateTime[] dateTimes)
{
long[] lngTicks = new long[dateTimes.Length];
long lngTotal = 0;

// Convert all DateTimes to ticks
for (int i = 0; i < dateTimes.Length; i++)
{
lngTicks = dateTimes.Ticks;
lngTotal += lngTicks;
}

// Return the average as a DateTime
return new DateTime(lngTotal / lngTicks.Length);
}

P.
 
Thanks Paul, i discovered the 'ticks' property shortly after posting.

Thanks for the quick reply anyway.

SN

Paul E Collins said:
sho_nuff said:
I have several DateTime objects and i want to
get the average of all of them.
What would be the easiest way to do this?

private static DateTime AverageDateTime(params DateTime[] dateTimes)
{
long[] lngTicks = new long[dateTimes.Length];
long lngTotal = 0;

// Convert all DateTimes to ticks
for (int i = 0; i < dateTimes.Length; i++)
{
lngTicks = dateTimes.Ticks;
lngTotal += lngTicks;
}

// Return the average as a DateTime
return new DateTime(lngTotal / lngTicks.Length);
}

P.
 
Back
Top