[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

convert double to hour:minutes vb.net

Posted on 2006-04-06
17
Medium Priority
?
2,969 Views
Last Modified: 2007-12-19
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
Comment
Question by:tentavarious
  • 5
  • 4
  • 4
  • +2
17 Comments
 
LVL 20

Expert Comment

by:ElrondCT
ID: 16392293
 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 43

Expert Comment

by:TimCottee
ID: 16392410
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
 
LVL 20

Expert Comment

by:ElrondCT
ID: 16392417
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 43

Accepted Solution

by:
TimCottee earned 500 total points
ID: 16392448
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16392695
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

by:WinterMuteUK
ID: 16392722
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

by:WinterMuteUK
ID: 16392736
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

by:tentavarious
ID: 16392966
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16393570
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

by:tentavarious
ID: 16394312
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 86

Expert Comment

by:Mike Tomlinson
ID: 16394738
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

by:tentavarious
ID: 16395559
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

by:ElrondCT
ID: 16395608
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
 
LVL 20

Expert Comment

by:ElrondCT
ID: 16395629
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

by:tentavarious
ID: 16395642
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 86

Expert Comment

by:Mike Tomlinson
ID: 16396176
@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

by:ElrondCT
ID: 16396202
Thanks for the info, Idle_Mind. I'm definitely learning as I go...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question