vb.net 两个日期时间之间的时间跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17074939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:55:21  来源:igfitidea点击:

TimeSpan between two DateTimes

vb.netvisual-studio-2012

提问by Lexy Feito

I'm trying to find out the time span between two dates.

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

What I have this so far:

到目前为止我有什么:

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

But I don't the the correct answer.

但我不知道正确答案。

回答by Douglas Barbin

This gives the time elapsed since Jan 1, 2013:

这给出了自 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.")

回答by matzone

You cannot use "stop", "end"as Variable .. This should be works (tested on VS2005)

你不能使用"stop", "end"作为变量 .. 这应该是有效的(在 VS2005 上测试)

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

回答by Andreas Karpinski

Looks fine to me.

对我来说看起来不错。

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

Result: 00:05:00

结果:00:05:00