本文介绍了计算时差,仅返回小时和分钟(在VB.net中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

示例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

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

示例:

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

结果将是:

datetime2 - datetime1 = -24 Hours 00 Minutes

谢谢!

推荐答案

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

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)

要将其扩展到秒:

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中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 19:48