TimeSpan在两个DateTime之间

时间:2013-06-12 20:40:51

标签: vb.net visual-studio-2012

我试图找出两个日期之间的时间跨度。

到目前为止我有这个:

 dim start as DateTime = now
 dim stop as DateTime = now
 dim span as TimeSpan = stop.Subtract(start)

但我没有正确答案。

3 个答案:

答案 0 :(得分:1)

这给出了自2013年1月1日起的时间:

Dim start as DateTime = #1/1/2013#
Dim stop as DateTime = Now
Dim elapsed As TimeSpan = start.Subtract(stop)

Console.Write(elapsed.Days & " days, " & elapsed.Hours & " hours, " & elapsed.Minutes & " minutes, " & elapsed.Seconds & " seconds.")

答案 1 :(得分:0)

对我来说很好。

E.g。

Dim startTime As DateTime = New DateTime(2013, 6, 12, 22, 45, 0)

Dim endTime As DateTime = New DateTime(2013, 6, 12, 22, 50, 0)

Dim span As TimeSpan = endTime - startTime

结果:00:05:00

答案 2 :(得分:0)

您无法使用“停止”“结束”作为变量..这应该可行(在VS2005上测试)

dim start as DateTime = now
dim stops as DateTime = now
dim span as TimeSpan = stops.Subtract(start)