计算时差并仅返回小时和分钟(在 VB.net 中)

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

Calculate time difference and return only hours and minutes (in VB.net)

vb.netvisual-studio-2010visual-studio

提问by Fariz Luqman

I'm developing a system to calculate time difference. How can I compare two time (with date) and get the hours & minutes of the difference?

我正在开发一个系统来计算时差。如何比较两个时间(带日期)并获得差异的小时数和分钟数?

Example 1:

示例 1:

datetime1 = 1-Apr-2014 01:05:04 AM
datetime2 = 1-Apr-2014 02:05:04 AM

Results will be:

结果将是:

datetime2 - datetime1 = 01 Hours 00 Minutes

Example 2:

示例 2:

datetime1 = 1-Apr-2014 01:05:04 AM
datetime2 = 2-Apr-2014 02:15:04 AM

Results will be:

结果将是:

datetime2 - datetime1 = 25 Hours 10 Minutes

A negative value (for datetime1 > datetime2) would also be helpful for indicating that datetime1 is greater than datetime2

负值(对于 datetime1 > datetime2)也有助于表明 datetime1 大于 datetime2

Example:

例子:

datetime1 = 2-Apr-2014 01:05:04 AM
datetime2 = 1-Apr-2014 01:05:04 AM

Results will be:

结果将是:

datetime2 - datetime1 = -24 Hours 00 Minutes

Thank you!

谢谢!

回答by Tomas Pastircak

My recommendation would be to use something like

我的建议是使用类似的东西

Dim minutesDiff = DateDiff(DateInterval.Minute,datetime1,datetime2)
Console.WriteLine("{0} hours {1} minutes", Math.Abs(minutesDiff) / 60, Math.Abs(minutesDiff) Mod 60)

To extend this to seconds:

将此扩展到秒:

Dim secondsDiff = DateDiff(DateInterval.Second,datetime1,datetime2)
Console.WriteLine("{0} hours {1} minutes {2} seconds", Math.Abs(secondsDiff)/3600, (Math.Abs(secondsDiff) Mod 3600) / 60, Math.Abs(secondsDiff) Mod 60)

回答by har07

Another way, you can use TotalHoursand Minutesproperty of TimeSpanobject resulted from subtracting two DateTimes :

另一种方式,你可以使用TotalHours和对象的Minutes属性TimeSpan减去两个DateTimes :

Dim d1 As DateTime = DateTime.Now
Dim d2 As DateTime = DateTime.Now.AddHours(25).AddMinutes(10)

Dim difference As TimeSpan = d2 - d1
'round down total hours to integer'
Dim hours = Math.Floor(difference.TotalHours)
Dim minutes = difference.Minutes
'Following line prints : 25 Hours 10 Minutes'
Console.WriteLine("{0} Hours {1} Minutes", hours, minutes)

回答by Fariz Luqman

Solutions to my question is:

我的问题的解决方案是:

Dim d1 As DateTime = DateTime.Parse("1-Apr-2014 10:10:00 AM")
Dim d2 As DateTime = DateTime.Parse("2-Apr-2014 10:10:00 AM")
Dim difference As TimeSpan = d2 - d1
'round down total hours to integer'
Dim hours = Math.Floor(difference.TotalHours)
Dim minutes = Math.Abs(difference.Minutes)
Dim seconds = difference.Seconds
Dim timeleft As String = Format(hours, "00") + " h " + Format(minutes, "00") + " m " + Format(seconds, "00") + " s "
If Int(seconds) < 0 Then
     timeleft = "00 h 00 m 00 s (Time Out)"
End If
Return timeleft 
' timeleft 24 hours 00 minutes 00 seconds