Results 1 to 9 of 9

Thread: Conversion to Time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Conversion to Time

    Hi... my simple code to convert a number to "h:nn:ss" works for just about any number, EXCEPT 6, 12, and 24.

    Let's assume you have Two Controls on a Form:

    txtTimeEntry.Text
    lblTimeReturn


    Now, here's the code:

    ------------------------------------------------

    Private Sub txtEnter_Change()

    lblTimeResult = txtEnter.Text / 24

    lblTimeResult = Format(lblTimeResult, "h:nn:ss")

    ------------------------------------------------

    This code works GREAT for any number you enter.

    If you enter 2.5, you'll see... 2:30:00
    If you enter 7.15, you'll see... 7:09:00

    It's a great way to convert any digit to an hour equivalent, EXCEPT when the digit equals 6, 12, or 24.

    If you enter 6, you'll see... 0:25:00 (when you want to see 6:00:00)
    If you enter 12, you'll see... 0:05:00 (when you want to see 12:00:00)
    If you enter 24, you'll see... 0:00:00 (when you want to see 24:00:00)

    Any ideas or examples of a better "digit-to-time" conversion code?

    THANKS!!!

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Conversion to Time

    Quote Originally Posted by balanj
    lblTimeResult = txtEnter.Text / 24
    I dont understand the logic of converting number to time like this...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: Conversion to Time

    I figured out a quick fix...

    I simply added .000000000001 to the Time conversion (done by dividing a number by 24). This alleviated the 6, 12, 24 problem. Only issue is that 12 & 24 get an extra minuted added. This is OK for my program's purposes...

    ------------------------------------------------

    Private Sub txtEnter_Change()

    lblTimeResult = (Val(txtEnter.Text) / 24) + .000000000001

    lblTimeResult = Format(lblTimeResult, "h:nn:ss")

    ------------------------------------------------

  4. #4
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Conversion to Time

    This works properly (as far as I can see)
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub txtEnter_Change()
    4.  
    5.     Dim ValText As Single
    6.    
    7.     ValText = Val(txtEnter.Text)
    8.     lblTimeResult.Caption = Format$(ValText / 24, "h:nn:ss")
    9.  
    10. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Conversion to Time

    pnish: you still might want to use a Double instead of a Single as Date is equivalent to Double (both work mathwise/binarywise the same way afaik). It should be possible to do:
    VB Code:
    1. Dim dtmTime As Date
    2.     dtmTime = Val(txtEnter.Text) / 24
    3.     lblTimeResult.Caption = FormatDateTime$(dtmTime, vbLongTime)
    Using correct datatypes is important

    If anyone wonders why this works: if you add one to a Date datatype (or a Double that is converted to Date), it will jump up by one day. Thus you can set any given period of time by X / 24 (think of it as Xth of a day).

  6. #6
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Conversion to Time

    Quote Originally Posted by Merri
    pnish: you still might want to use a Double instead of a Single as Date is equivalent to Double (both work mathwise/binarywise the same way afaik).
    Quite right of course Val() also returns a double.
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: Conversion to Time

    Merri and pnish,

    Thanks for your replies, your code solutions are great, however I run into a couple of issues.

    If the txtTimeEnter.Text digit is GREATER than 24, the clock starts over... (when I only want to show the total hours)

    For Example, I want 31 hours, 18 minutes, 26 seconds to show up as... 31:18:26
    (The code we currently are using show displays... 7:18:26 AM)

    I get around this with the following code:

    -----------------------------------------------------------------------

    Private Sub txtEnter_Change()

    lblTimeResult = (Val(txtEnter.Text) / 24) + .000000000000001

    lblTimeResult = Int(lblTimeResult * 24) & ":" & Format(lblTimeResult, "nn:ss")

    -----------------------------------------------------------------------


    This code allows me to show a total hours past 24 (past 1 day) and eliminates the AM or PM after the time total.

    The total decimal places for (.000000000000001) MUST equal 15 places exactly to maintain the proper minute count. (I don't know exactly why... only discovered through trial and error)

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Conversion to Time

    VB Code:
    1. Private Sub Text1_Change()
    2.     Dim dtmTest As Date
    3.     dtmTest = Abs(Val(Text1.Text)) / 24
    4.     Me.Caption = Format$(DateDiff("h", CDate(0), dtmTest), "0:") & Format$(dtmTest, "nn:ss")
    5. End Sub

    Please, don't use a label to store the data as a string. The use of incorrect datatype is why you have to add the .000000000000001 cheat in your current code. My code above works just fine without any cheats, because it uses the correct datatype and no coercions ("automatic conversions") happens in the code. Besides giving you headache on "not working as expected", coercions slow down your code, although it doesn't matter in this case just as much.

    Also, don't rely on default object properties: if you want to change a caption of a label, use Label.Caption. This for two reasons: code with .Caption executes faster and also use of .Caption makes the code more readable.


    Dates (and Doubles) are a floating point datatype. The way they work internally ("float" the decimal point within an integer number) is the problem you encountered and how a floating point number is converted into a date.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    83

    Re: Conversion to Time

    Thanks Merri!!!

    Wow, that code snippet works perfectly... and you're right, I'm now not "cheating" to get the data.

    Also, my form does load faster and operates more efficiently.

    THANKS AGAIN!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width