Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

math

vb.net

vb6

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

like image 752
Neal Avatar asked Nov 09 '10 16:11

Neal


People also ask

How do you convert minutes to hours format?

There are 60 minutes in 1 hour. To convert from minutes to hours, divide the number of minutes by 60. For example, 120 minutes equals 2 hours because 120/60=2.

How do you convert minutes to hours and seconds?

Converting between hours, minutes, and seconds using decimal time is relatively straightforward: time in seconds = time in minutes * 60 = time in hours * 3600. time in minutes = time in seconds / 60 = time in hours * 60. time in hours = time in minutes / 60 = time in seconds / 3600.


2 Answers

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!

like image 142
Will Marcouiller Avatar answered Sep 22 '22 03:09

Will Marcouiller


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

like image 29
MarkJ Avatar answered Sep 19 '22 03:09

MarkJ