|
-
Jul 19th, 2006, 06:56 AM
#8
Re: Conversion to Time
VB Code:
Private Sub Text1_Change()
Dim dtmTest As Date
dtmTest = Abs(Val(Text1.Text)) / 24
Me.Caption = Format$(DateDiff("h", CDate(0), dtmTest), "0:") & Format$(dtmTest, "nn:ss")
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|