Average DateTime

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));
}

1

Contributor

0

Replies

3

Views

Are you able to help answer this sponsored question?

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Have something to contribute to this discussion?
Please be thoughtful, detailed and courteous, and
be sure to adhere to our posting rules.