I need to calculate the average of the date difference of all of my rows in dataGridView
.
I implemented NodaTime (which is far more easy than the traditional methods to calculate date difference) and, I did this just to try out:
var date1 = new LocalDate(2013, 1, 29);
var date2 = new LocalDate(2018, 1, 23);
Period period = Period.Between(date1, date2.PlusDays(1));
label1.Text = string.Format("{0} anos, {1} meses e {2} dias", period.Years,
period.Months, period.Days);
Now, what I do to get both dates of the dataGridView
, calculate in each row the date difference and with all calculate the average? I want it to show in years, months and days. Thanks.
Unfortunately there's no real concept of an average of Period
values, as they're not even directly comparable. For example, is "1 month" longer or shorter than "29 days"? It depends on the month. Moving to an average, what's the of the two periods "1 month" and "29 days"? There's nothing really obvious as a useful answer to that question, IMO.
What you could do is get the difference just in days (probably using Period.Between
and specifying PeriodUnits.Days
) - then find the average number of days. That makes logical sense and is a lot more easily defined than the average of years/months/days.
See more on this question at Stackoverflow