C# 无法识别为有效的日期时间。从索引0开始的未知单词

C# 无法识别为有效的日期时间。从索引0开始的未知单词,c#,C#,我正在尝试计算两个列表框之间的时间间隔。 这是我的密码 for (int x = 0; x < listBox1.Items.Count; x++) { DateTime z = DateTime.Parse(listBox2.Items.ToString()); DateTime c = DateTime.Parse(listBox3.Items.ToString()); TimeSpan w = c - z; } for(int x=0;x

我正在尝试计算两个列表框之间的时间间隔。 这是我的密码

for (int x = 0; x < listBox1.Items.Count; x++)
{
    DateTime z = DateTime.Parse(listBox2.Items.ToString());
    DateTime c = DateTime.Parse(listBox3.Items.ToString());
    TimeSpan w = c - z;
}
for(int x=0;x
也许你需要这样的东西:

for (int x = 0; x < listBox1.Items.Count; x++)
{
    DateTime z = DateTime.Parse(listBox2.Items[x].ToString());
    DateTime c = DateTime.Parse(listBox3.Items[x].ToString());
    TimeSpan w = c.Subtract(z);
} 
for(int x=0;x

此外,您的列表框项目应具有相同的计数

项目
是一个集合,
ToString
将为您提供类型名称,而不是集合中的值。请看我下面的回答,我认为您需要索引器来获取for循环中的项目而不是项目集合。此外,为什么在
listBox1.Items
的索引上迭代x,然后使用
listBox2
listBox3
?所有3个列表框是否都保证具有相同数量的项目?最后,您将不断地将差异分配给
w
,最终它只等于最后两个日期之间的差异。使用索引是有意义的,但它仍然基本上只使用最后一个项目。