-2

I have 3 datetime values in hh:mm:ss format. how can I calculate the average of these values in c# ?

DateTime casaDate = DateTime.Parse(casaAvgTAT, System.Globalization.CultureInfo.CurrentCulture);
DateTime ACDate = DateTime.Parse(ACAvgTAT, System.Globalization.CultureInfo.CurrentCulture);
DateTime TermDate = DateTime.Parse(termAvgTAT, System.Globalization.CultureInfo.CurrentCulture);
derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • u better search out of it and please edit your answer. https://stackoverflow.com/questions/47227676/how-to-calculate-average-of-some-timespans-in-c-sharp/47227754 – BerkGarip May 23 '21 at 08:28
  • What would the average of three points in time be? You probably mean [`TimeSpan`](https://learn.microsoft.com/en-us/dotnet/api/system.timespan?view=net-5.0) (ie durations). – derpirscher May 23 '21 at 08:33
  • 2
    Does this answer your question? [How to calculate average of some timespans in C#](https://stackoverflow.com/questions/47227676/how-to-calculate-average-of-some-timespans-in-c-sharp) – derpirscher May 23 '21 at 08:35

1 Answers1

2

While I'm not sure what sense it makes to average a set of datetime (because they are points in time), you can average their ticks and convert back to datetime

new DateTime(new[]{casaDate.Ticks, ACDate.Ticks, TermDate.Ticks}.Average());

Be aware that this might not result in what you expect: averaging 6am jan 1 1999, 12 noon jan 1 2000 and 6pm jan 1 2001 doesn't result in 12 noon jan 1 2000..

I'm also not really sure what you mean by "in hh:mm:ss format" - datetime don't have formats, string representations of datetimes have formats..

Caius Jard
  • 72,509
  • 5
  • 49
  • 80