Turn "200" into "3h 20m"

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Greetings,

I am attempting to take a number that is currently in minutes and turn it
into a string that will list the hours followed by minutes.

Some more examples:

"30" into "0h 30m"
"155 into "2h 35m"
"180 into "3h 0m"

Is this possible? How can I do it? Thanks,

-Dave
 
Dave,

Dim Hours As Integer = 0 ' Always start at 0 hours
Dim Minutes As Integer = 155 ' # of minutes to convert from

Do While Minutes >= 60
Hours += 1
Minutes -= 60
Loop

Return Hours & "h " & Minutes & "m"

Phil
 
Minutes \ 60 to get hours
Minutes MOD 60 gets you the remaining minutes

Hours & "h " & minutes & "m"

Matt
 
Dim StartingMinutes As Integer = 155

Dim DT As New TimeSpan(0, StartingMinutes, 0)

MessageBox.Show(DT.Hours & "h" & " " & DT.Minutes & "m")

Hope it helps
Chris
 
Scratch that.

I haven't worked with TimeSpan in .NET, and so my solution stinks.

Chris' solution is much better.
 
Chris & Dave,
You can also use TimeSpan.FromMinutes:

Dim ts As TimeSpan = TimeSpan.FromMinutes(StartingMinutes)

There are also FromDays, FromHours, FromMilliseconds, FromSeconds &
FromTicks.

I find the above From* methods to be a little more explicit on what is being
converted, rather then trying to remember if the 3 parameter constructor is
"days, hours, minutes" or "hours, minutes, seconds"... Otherwise the result
is the same!

The cool thing (more important thing?) about the From* methods is they take
a Double, so you can say:

Dim ts As TimeSpan = TimeSpan.FromHours(1.5)

NOTE: FromTicks take a Long as a fraction of a Tick doesn't specifically
make sense...

Hope this helps
Jay
 
Good point, never looked at those methods before.

So many methods so little time.
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top