Best way to convert total minutes into HH:mm format?
Solution 1
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!
Solution 2
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
Solution 3
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
Solution 4
Take modulus 60, then integer-divide by 60.
In VB, integer division uses \
.
Solution 5
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.
Neal
Updated on September 29, 2020Comments
-
Neal over 3 years
I get a return value from a web service in minutes, for example 538 minutes. I need to break this down in hours and minutes. 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
-
DennyRolling over 13 yearsyou can save one division by integer-divide, then multiply back and substract: 538/60=8, 538-8*60=58, thus result is 8:58 unsure how critical it is nowadays but division used to be quite slow.
-
MarkJ over 13 yearsI think Neal wants leading zeros.
CType(hours,String)
won't give leading zeros if hours < 10 -
clausvdb over 13 yearsForget that issue. True, division usually does take some more clock cycles than multiplication. Nothing to worry about in high level languages, though, since it is all peanuts compared to all the overhead even modern compilers produce. I guess that your solution even will be slower given the overhead produced by the sequent mul and sub, but I would never bother benchmarking unless you consider looping the calculations a billion times per second or more.
-
Alejandro Lasebnik over 6 years@MarkJ you are right. In order to retrieve the correct time with leading zero in the needed case, you need to use Cstr and PadLeft in the following way: Cstr(hours).PadLeft(2, "0"c) & ":" & Cstr(minutes).PadLeft(2, "0"c)
-
Daniel Gee almost 4 yearsThe second code doesn't work. You get an error: System.ArgumentOutOfRangeException: 'Hour, Minute, and Second parameters describe an un-representable DateTime.'