I had to resolve a similar issue for one of our projects. I've knocked up the below code snippet which seems to work.
Assuming you have a grid on a form for the purpose of this test.
Add the dates to your grid columns. I use columns 0 and 1 for the 2 dates.
private void Form1_Load(object sender, EventArgs e)
{
dgGridView.Rows.Add(new DataGridViewRow());
dgGridView.Rows.Add(new DataGridViewRow());
dgGridView.Rows.Add(new DataGridViewRow());
dgGridView.Rows[0].Cells[0].Value = "Feb 01 2018 00:00:00";
dgGridView.Rows[0].Cells[1].Value = "Feb 03 2018 06:00:45";
dgGridView.Rows[1].Cells[0].Value = "Feb 02 2018 17:00:00";
dgGridView.Rows[1].Cells[1].Value = "Feb 03 2018 21:54:21";
dgGridView.Rows[2].Cells[0].Value = "Feb 04 2017 10:00:00";
dgGridView.Rows[2].Cells[1].Value = "Feb 07 2018 08:23:26";
}
The under a button click or whatever mechanism you choose, calculate the averages. This is done by calculating the seconds that have elapsed between each date range and then divide it by the number of rows.
var totalSeconds = 0.0;
foreach (DataGridViewRow row in dgGridView.Rows)
{
var date1 = Convert.ToDateTime(row.Cells[0].Value);
var date2 = Convert.ToDateTime(row.Cells[1].Value);
var diff = (date2 - date1).TotalSeconds;
row.Cells[2].Value = diff;
totalSeconds += diff;
}
var totalRows = 3;
var aveSeconds = totalSeconds / totalRows;
TimeSpan aveTime = TimeSpan.FromSeconds(aveSeconds);
var totDays = aveTime.Days;
var totHours = aveTime.Hours;
var totMins = aveTime.Minutes;
var totSeconds = aveTime.Seconds;
var ave = $"Days:{totDays} Hours:{totHours} Mins:{totMins} Seconds:{totSeconds}";
You can then get a TimeSpan object from those total seconds and extract the number of days, hours, minutes and seconds for the average.
I think this works. Apologies in advance if some eagle eyed person spots a flaw, but I hope it helps you on your way.
Thanks