将总分钟转换为HH:mm格式的最佳方法?

时间:2010-11-09 16:05:40

标签: vb.net math vb6

我在几分钟内从网络服务获得一个返回值,例如538分钟。我需要在几小时和几分钟内解决这个问题。什么是最快的方法,在.net代码和VB6代码(两个应用程序使用该服务)将其从分钟转换为HH:mm?

由于

6 个答案:

答案 0 :(得分:12)

此代码应该在.NET和VB6中都有效:

Dim hours As Integer = 538 \ 60
Dim minutes As Integer = 538 - (hours * 60)
Dim timeElapsed As String = CType(hours, String) & ":" & CType(minutes, String)
label1.Text = timeElapsed

在.NET中,您应该能够执行以下操作(需要进行测试):

Dim timeElapsed As DateTime = New DateTime(1, 1, 1, 0, 538, 0)
label1.Text = timeElapsed.ToString("HH:mm")

我希望这有帮助!

答案 1 :(得分:5)

在.Net中,您有一个TimeSpan类,因此您可以执行以下操作

Dim t As New TimeSpan(0, 538, 0)

'Then you have the 2 properties
t.Hours
t.Minutes

答案 2 :(得分:5)

在VB6中,您可以使用Format(538/1440.0, "hh:mm")

VB6 Date值可以被视为天数,一天中有1440分钟。因此,538/1440是您所期间的天数,然后您可以使用Format

答案 3 :(得分:3)

取模数60,然后整数除以60。

在VB中,整数除法使用\

答案 4 :(得分:2)

如果需要对其他DateTime对象执行操作,则可能需要使用TimeSpan对象,例如

    Dim oTS As New TimeSpan(0, 538, 0)
    MessageBox.Show(Format(oTS.Hours, "00") & ":" & Format(oTS.Minutes, "00"))
    Dim startime As DateTime = Date.Now
    Dim newtime As DateTime = startime + oTS
    MessageBox.Show(newtime.ToString("HH:mm"))

如果没有,那么Matthew建议使用Integer division'\'和modulo'Mod'将会很好用。

答案 5 :(得分:0)

如果你需要字符串:     dt.ToString( “HH:MM”);

(info)