# convert double to hour:minutes vb.net

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.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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)
Hi tentavarious,

Or even easier:

Tim Cottee
Commented:
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))

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
Commented:
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.
Commented:
That's originally from:

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

Which is Idle_Mind as well :)

Wint.
Author Commented:
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
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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...
Author Commented:
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.
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's no biggee...Tim can keep the points.

I just wanted to make sure you knew the limitations of your approach.  =)
Author Commented:
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?
Commented:
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.
Commented:
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.
Author Commented:
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
High School Computer Science, Computer Applications, and Mathematics TeachersCommented:
@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.
Commented:
Thanks for the info, Idle_Mind. I'm definitely learning as I go...
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.