Question:

Visual Basic: How to convert double format to time?

by  |  earlier

0 LIKES UnLike

Hello,

I am using VB6.0 and consider myself to pretty good in it, however, I have stumbled across a problem which really eludes me. I need to convert a double format to a time format.

Let me give you an example

====================

lpResTotal(0) = "123/1000"

lpResTotal(1) = "145/1230"

lpResTotal(2) = "999/10239"

lpResTotal(3) = "1230/1230"

lpResPerHour(0) = "330"

lpResPerHour(1) = "390"

lpResPerHour(2) = "130"

lpResPerHour(3) = "3300"

Dim sSplitTest() As String

For i = 0 To 3

sSplitTest() = Split(lblResTotal(i).Caption, "/")

If sSplitTest(0) <> sSplitTest(1) Then

' Firstly, let's get the current count of resources

lpCurAmmount = sSplitTest(1) - sSplitTest(0)

' Let's divide this by the resources per hour to give us how many hours are needed

lpResNeeded_hour = Int(lpCurAmmount / Replace(Replace(lblResPerHour(i).Caption... "(+ ", ""), " / hour)", ""))

lpResNeeded_mins = "Don't know what this should be"

lpResNeeded_secs = "Don't know what is supposed to be here"

lblResFillTime(i).Caption = lpResNeeded_hour & ":" & lpResNeeded_mins & ":" lblResNeeded_secs

Else

lblResFillTime(i).Caption = "Res finished"

End If

Next i

====================

Let me show you what you should be seeing:

Example:

lpResTotal = "317/1200"

lpResFin = 2:40:06

 Tags:

   Report

2 ANSWERS


  1. Your information is somewhat lacking... how do you get to hours from the numbers listed?

    What&#039;s in the captions? What does your code replace? Yahoo! clips long uninterrupted lines, so part of your code is useless.

    In your example, &quot;317/1200&quot; indicates what, exactly? How is it supposed to translate to 2:40:06?

    When working with numbers and times, I usually convert used time to a number of seconds, and use DateAdd to add to a standard start time to come up with a result in hours:minutes:seconds.

    If so required, I can then take a goal time using the same method, and use a DateDiff to show the difference.

    In both cases the result is a number in Date format, which can then be displayed arbitrarily using VB&#039;s Format function...

    resTime = Format ( resultTime, &quot;HH:nn:ss&quot;)


  2. It appears that you have the solution mostly done.  For minutes and seconds, simply multiply the &quot;hours&quot; fraction by 60 time units (e.g. 60 minutes per hour, 60 seconds per minute) and use the integer portion for your intermediate total.  See  code modifications below:

    lpResTotal(0) = &quot;123/1000&quot;

    lpResTotal(1) = &quot;145/1230&quot;

    lpResTotal(2) = &quot;999/10239&quot;

    lpResTotal(3) = &quot;1230/1230&quot;

    lpResPerHour(0) = &quot;330&quot;

    lpResPerHour(1) = &quot;390&quot;

    lpResPerHour(2) = &quot;130&quot;

    lpResPerHour(3) = &quot;3300&quot;

    Dim sSplitTest() As String

    For i = 0 To 3

    sSplitTest() = Split(lblResTotal(i).Caption, &quot;/&quot;)

    If sSplitTest(0) &lt;&gt; sSplitTest(1) Then

    &#039; Firstly, let&#039;s get the current count of resources

    lpCurAmount = sSplitTest(1) - sSplitTest(0)

    &#039; Let&#039;s divide this by the resources per hour to give us how many hours are needed

    temp_time_calc = lpCurAmount / Replace(Replace(lblResPerHour(i).Caption... &quot;(+ &quot;, &quot;&quot;), &quot; / hour)&quot;, &quot;&quot;)

    lpResNeeded_hour = Int(temp_time_calc)

    &#039; Calculate the fractional hours and convert to minutes

    temp_time_calc = (temp_time_calc - lpResNeeded_hour)*60

    lpResNeeded_mins = Int(temp_time_calc)

    &#039; Calculate the fractional minutes and convert to seconds

    temp_time_calc = (temp_time_calc - lpResNeeded_mins)*60

    lpResNeeded_secs = Int(temp_time_calc)

    lblResFillTime(i).Caption = lpResNeeded_hour &amp; &quot;:&quot; &amp; lpResNeeded_mins &amp; &quot;:&quot; lblResNeeded_secs

    Else

    lblResFillTime(i).Caption = &quot;Res finished&quot;

    End If

    Next i

    Hope this helps!  Let me know if you need any additional explanation...

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.