计算多行的平均日期差异
Calculate average date difference of multiple rows
我需要计算 dataGridView
中所有行的日期差异的平均值。
我实现了 NodaTime(这比计算日期差异的传统方法容易得多),我这样做只是为了尝试:
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);
现在,我如何获取 dataGridView
的两个日期,在每一行中计算日期差异并计算平均值?我希望它以年、月和日显示。谢谢
我不得不为我们的一个项目解决类似的问题。我编写了以下似乎有效的代码片段。
假设您在表单上有一个用于此测试的网格。
将日期添加到您的网格列中。我将第 0 列和第 1 列用于 2 个日期。
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";
}
在单击按钮或您选择的任何机制下,计算平均值。这是通过计算每个日期范围之间经过的秒数然后除以行数来完成的。
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}";
然后您可以从这些总秒数中获取一个 TimeSpan 对象,并提取平均天数、小时数、分钟数和秒数。
我认为这行得通。如果有眼尖的人发现了瑕疵,请提前致歉,但希望对您有所帮助。
谢谢
不幸的是,Period
值的平均值没有真正的概念,因为它们甚至不能直接比较。例如,“1 个月”比“29 天”长还是短?这取决于月份。移动到平均值,“1 个月”和“29 天”这两个时间段是多少?对于这个问题,没有什么明显的有用答案,IMO。
您可以做的是几天(可能使用Period.Between
并指定PeriodUnits.Days
) - 然后找到平均天数。 符合逻辑,而且比 years/months/days 的平均值更容易定义。
我需要计算 dataGridView
中所有行的日期差异的平均值。
我实现了 NodaTime(这比计算日期差异的传统方法容易得多),我这样做只是为了尝试:
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);
现在,我如何获取 dataGridView
的两个日期,在每一行中计算日期差异并计算平均值?我希望它以年、月和日显示。谢谢
我不得不为我们的一个项目解决类似的问题。我编写了以下似乎有效的代码片段。
假设您在表单上有一个用于此测试的网格。
将日期添加到您的网格列中。我将第 0 列和第 1 列用于 2 个日期。
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";
}
在单击按钮或您选择的任何机制下,计算平均值。这是通过计算每个日期范围之间经过的秒数然后除以行数来完成的。
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}";
然后您可以从这些总秒数中获取一个 TimeSpan 对象,并提取平均天数、小时数、分钟数和秒数。
我认为这行得通。如果有眼尖的人发现了瑕疵,请提前致歉,但希望对您有所帮助。
谢谢
不幸的是,Period
值的平均值没有真正的概念,因为它们甚至不能直接比较。例如,“1 个月”比“29 天”长还是短?这取决于月份。移动到平均值,“1 个月”和“29 天”这两个时间段是多少?对于这个问题,没有什么明显的有用答案,IMO。
您可以做的是几天(可能使用Period.Between
并指定PeriodUnits.Days
) - 然后找到平均天数。 符合逻辑,而且比 years/months/days 的平均值更容易定义。