开发者

Best way to convert total minutes into HH:mm format?

I get a return value from a web service in minutes, for example 538 minutes. I need to break this down in hours and 开发者_如何学Cminutes. What is the fastest way, in .net code and also VB6 code (two apps use the service) to convert this from minutes to HH:mm?

Thanks


This code should work both in .NET and 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

In .NET exclusively, you should be able to do the following (which requires to be tested):

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

I hope this helps!


In .Net you have a TimeSpan class so you can do the following

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

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


In VB6 you could just use Format(538/1440.0, "hh:mm")

VB6 Date values can be treated as a number of days, and there's 1440 minutes in a day. So 538/1440 is the number of days in your period, and then you can use Format


Take modulus 60, then integer-divide by 60.

In VB, integer division uses \.


If you need to perform operations with other DateTime objects it might be useful to use a TimeSpan object instead, e.g.

    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"))

If not then Matthew's suggestion of using Integer division '\' and modulo 'Mod' will work very well.


if you need strings then: dt.ToString("HH:mm");

(info)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜