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

示例 1:

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

结果将是:
datetime2 - datetime1 = 01 Hours 00 Minutes

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

结果将是:
datetime2 - datetime1 = 25 Hours 10 Minutes

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

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

结果将是:
datetime2 - datetime1 = -24 Hours 00 Minutes

谢谢!

最佳答案

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

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

将此扩展到秒:
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)

关于vb.net - 计算时差并仅返回小时和分钟(在 VB.net 中),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22879310/

10-12 12:49