Solved

# convert double to hour:minutes vb.net

Posted on 2006-04-06
2,560 Views
Hello, I am reading in a double value which is in minutes like so 432.45, I need to convert this value
to Hours : Minutes, I got it working but there must be an easier way.  Also I am reading in a smaller double that I need to convert to Minutes : seconds for example : 23.5 in minutes is 23 minutes 30 seconds.
0
Question by:tentavarious

LVL 20

Expert Comment

Dim dblInput as Double
Dim intHours as Integer
Dim intMinutes as Integer
Dim intSeconds as Integer

intHours = Cint(dblInput / 60)
intMinutes = Cint(Mod(dblInput, 60))
intSeconds = Cint((dblInput - Int(dblInput)) * 60)
0

LVL 44

Expert Comment

Hi tentavarious,

Or even easier:

Tim Cottee
0

LVL 20

Expert Comment

Oops, scratch that. Cint rounds, rather than truncating. So it should be:

intHours = Cint(Int(dblInput / 60))
intMinutes = Cint(Int(Mod(dblInput, 60)))
intSeconds = Cint(Int((dblInput - Int(dblInput)) * 60))

0

LVL 44

Accepted Solution

tentavarious,

Sorry, the second one was for seconds. In fact you can use the first example for any value of minutes or tweak it like this just to show minutes and seconds

Tim
0

LVL 85

Expert Comment

Using a TimeSpan...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(Duration(0, 432.45, 0))
MsgBox(Duration(0, 23.5, 0))
End Sub

Private Function Duration(ByVal hours As Double, ByVal minutes As Double, ByVal seconds As Double) As String
Dim ts As New TimeSpan((hours * TimeSpan.TicksPerHour) + (minutes * TimeSpan.TicksPerMinute) + (seconds * TimeSpan.TicksPerSecond))
Return ts.TotalHours.ToString("0") & ":" & ts.Minutes.ToString("00") & ":" & ts.Seconds.ToString("00")
End Function
0

LVL 9

Expert Comment

I'm not sure what the correct syntax in VB is to do the display stuff etc, but the TimeSpan thing should be the same, and that will give you a TimeSpan of the hours and minutes you're after.

double hours = 9.25;
TimeSpan ts = new TimeSpan((long)(TimeSpan.TicksPerHour * hours));
string time = ts.Hours + ":" + ts.Minutes.ToString("00");
MessageBox.Show(time);

Cheers

Wint.
0

LVL 9

Expert Comment

That's originally from:

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21748803.html

Which is Idle_Mind as well :)

Wint.
0

Author Comment

Thanks TimCotte,
I thought there would be a function i could use.

Function gethours(ByVal stringval As String)
Dim hours As Double = Val(stringval)
Dim shold As String
If Val(stringval) > 0 Then
Else
shold = "00:00"
End If
Return shold
End Function

Function getminutes(ByVal stringval As String)
Dim mhold As Double = Val(stringval)
Dim shold As String
If Val(stringval) > 0 Then
Else
shold = "00:00"
End If
Return shold
End Function
0

LVL 85

Expert Comment

In your ToString() call, you should be using "HH" instead of "hh".  Otherwise you would get a "01" instead of "13" hours.

Also, since you are using a DateTime, which is returned from TimeSerial(), you can't go over 24 hours.  Using the TimeSpan approach in my example it will return the correct number of hours regardless of the duration...
0

Author Comment

Thanks idle, I quess I chose to quickly, but if we ever have more then 24 hours of downtime our company will have bigger problems then a computer app.  Anyway I can redistribute points?  I think I should split between you and tim.
0

LVL 85

Expert Comment

It's no biggee...Tim can keep the points.

I just wanted to make sure you knew the limitations of your approach.  =)
0

Author Comment

Actually I am running into a problem 77.8 minutes is being converted to 10:48 when it should be 77 minutes 48 seconds.
Any ideas?
0

LVL 20

Expert Comment

Is that 10:48 or 17:48? If the latter, the problem is that you're not including hh in your display string, so the hours aren't being shown. Change to

and you should get what you want.
0

LVL 20

Expert Comment

OTOH, if you want to see it as 77 minutes, rather than 1 hour, 17 minutes, you need to use the calculation method I listed up top. TimeSpan and DateTime variables aren't going to allow a number of minutes greater than 59.
0

Author Comment

Figured it out after i sent it.  I wanted to use TotalMinutes instead of Minutes.  I am actually converting two seperate values one value needs to me converted from minutes to Hours:minutes the other value needs to be converted to Minutes:seconds

Private Function Duration(ByVal hours As Double, ByVal minutes As Double, ByVal seconds As Double, ByVal bool As Boolean) As String
'This procedure is used to convert the time in minutes to hh:mm:ss
Dim ts As New TimeSpan((hours * TimeSpan.TicksPerHour) + (minutes * TimeSpan.TicksPerMinute) + (seconds * TimeSpan.TicksPerSecond))
Dim shold As String
If bool Then
shold = ts.TotalHours.ToString("0") & ":" & ts.Minutes.ToString("00")
Else
shold = ts.TotalMinutes.ToString("00") & ":" & ts.Seconds.ToString("00")
End If
Return shold
End Function
0

LVL 85

Expert Comment

@ElrondCT,

"TimeSpan and DateTime variables aren't going to allow a number of minutes greater than 59"

The Hours() and Minutes() properties yes...

...but the TotalHours() and TotalMinutes() properties can hold values greater than 59.
0

LVL 20

Expert Comment

Thanks for the info, Idle_Mind. I'm definitely learning as I go...
0

## Featured Post

### Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…