I got yesterday a question how to get average date from list of dates, and here's my answer.
Average DateTime
kvprajapati commented: definitely, sum of ticks. +10
static void Main(string[] args)
{
//Initializes new List of DataTime Object.
List<DateTime> dates = new List<DateTime>();
//Fills the List of DateTime Object.
for (int i = 0; i < 5; i++)
{
dates.Add(DateTime.Now); //Adds new DataTime Object in the list of DateTime Object.
Thread.Sleep(1000);//stop filling dates for one second
}
//Prints the List of DataTime Object.
for (int i = 0; i < 5; i++)
{
Console.WriteLine(dates[i]);
}
//Calls the method to get the average DataTime Object.
GetAverageDateTime(dates);
}
static void GetAverageDateTime(List<DateTime> dates)
{
//Total number of Ticks
long totalTicks = 0;
//Average number of Ticks
long averageTicks = 0;
//Gets the total of the List of DateTime Object Ticks.
for (int i = 0; i < dates.Count; i++)
{
totalTicks += dates[i].Ticks;
}
//Gets the average of Ticks (Average is the total divided by the number :))
averageTicks = totalTicks / dates.Count;
//Initializes new DateTime Object by the AverageTick object.
DateTime averageDate = new DateTime(averageTicks);
//Prints it to the user
Console.WriteLine(string.Format("Average date {0}", averageDate));
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.