vb.net 以秒为单位找出两个日期时间之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12529127/
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-09 16:17:54  来源:igfitidea点击:

find the difference between two datetimes in seconds

vb.netvisual-studiovisual-studio-2005

提问by Pezzzz

I have had a quick look around and I can't find an answer to this in vb.net or something that I can convert into vb.net.

我快速浏览了一下,但在 vb.net 或我可以转换为 vb.net 的东西中找不到答案。

I have two DateTimes in vb.net's 'Date' class. I would like to find the difference between these in seconds. I can do a-b, but the answer will still be a 'date'. I can use .seconds.minutesetc. and multiply but I will hit problems when I come to months.

我在 vb.net 的“Date”类中有两个 DateTime。我想在几秒钟内找到它们之间的区别。我可以做 ab,但答案仍然是“约会”。我可以使用.seconds.minutesetc. 和乘法,但是当我到几个月时会遇到问题。

Is there a simple way to do this, or do I need to write some elaborate-ish code?

有没有一种简单的方法可以做到这一点,或者我是否需要编写一些精心设计的代码?

Many Thanks

非常感谢

回答by Oded

Subtractthe DateTimevalues from each other - the returned type will be a TimeSpan.

DateTime值相互减去- 返回的类型将是 a TimeSpan

Get the TotalSecondsvalue from it.

TotalSeconds从中获取价值。

(date1 - date2).TotalSeconds

回答by KekuSemau

There actually is also a function made for this DateDiff(DateInterval.Second, d1, d2)

实际上还有一个功能是为此 DateDiff(DateInterval.Second, d1, d2)

回答by Adriaan Stander

How about something like

怎么样的东西

Dim secs As Double = DateTime.Today.Subtract(DateTime.Today.AddDays(-1)).TotalSeconds

Look at DateTime.Subtract Method (DateTime)and TimeSpan.TotalSeconds Property

查看DateTime.Subtract Method (DateTime)TimeSpan.TotalSeconds 属性