如何找到DateTime值的数组中的平均日期/时间 [英] How to find an average date/time in the array of DateTime values

查看:77
本文介绍了如何找到DateTime值的数组中的平均日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有日期时间值的数组:

 列表<&日期时间GT; arrayDateTimes; 



什么是找到它们之间的平均日期时间的方式吗?



举例来说,如果我有:

  2003  - 月 -  21 15:00:00 
2003 - 月 - 21 19:00:00
2003年五月21 20:00:00

平均应该是:

  2003  - 月 -  21 18:00:00 


解决方案

如果你有大的列表,你可以使用下面的方法。

 变种数= dates.Count; 
双TEMP = 0D;
的for(int i = 0; I<计数;我++)
{
温度+ =日期[I] .Ticks /(双)计数;
}
VAR平均=新的DateTime((长)温度);


If I have an array of DateTime values:

List<DateTime> arrayDateTimes;

What's the way to find the average DateTime among them?

For instance, if I have:

2003-May-21 15:00:00
2003-May-21 19:00:00
2003-May-21 20:00:00

the average should be:

2003-May-21 18:00:00

解决方案

If you have large list you can use below method

var count = dates.Count;
double temp = 0D;
for (int i = 0; i < count; i++)
{
    temp += dates[i].Ticks / (double)count;
}
var average = new DateTime((long)temp);

这篇关于如何找到DateTime值的数组中的平均日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆