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.
tentavariousAsked:
Who is Participating?
 
TimCotteeHead of Software ServicesCommented:
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

        MsgBox(TimeSerial(0, 0, 0).AddMinutes(432.25))
        MsgBox(TimeSerial(0, 0, 0).AddMinutes(23.5).ToString("mm:ss"))

Tim
0
 
ElrondCTCommented:
 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
 
TimCotteeHead of Software ServicesCommented:
Hi tentavarious,

Or even easier:

        MsgBox(TimeSerial(0, 0, 0).AddMinutes(432.25))
        MsgBox(TimeSerial(0, 0, 0).AddSeconds(23.5).ToString("MM:ss:ffff"))

Tim Cottee
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ElrondCTCommented:
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))

Sorry about any confusion.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
WinterMuteUKCommented:
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
 
WinterMuteUKCommented:
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
 
tentavariousAuthor 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
            shold = TimeSerial(0, 0, 0).AddMinutes(hours).ToString("hh:mm")
        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
            shold = TimeSerial(0, 0, 0).AddMinutes(mhold).ToString("mm:ss")
        Else
            shold = "00:00"
        End If
        Return shold
    End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
tentavariousAuthor 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's no biggee...Tim can keep the points.

I just wanted to make sure you knew the limitations of your approach.  =)
0
 
tentavariousAuthor 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?
0
 
ElrondCTCommented:
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

MsgBox(TimeSerial(0, 0, 0).AddMinutes(77.8).ToString("hh:mm:ss"))

and you should get what you want.
0
 
ElrondCTCommented:
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
 
tentavariousAuthor 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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
@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
 
ElrondCTCommented:
Thanks for the info, Idle_Mind. I'm definitely learning as I go...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.