|
-
Jul 18th, 2006, 11:54 PM
#1
Thread Starter
Lively Member
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!!!
-
Jul 18th, 2006, 11:56 PM
#2
Re: Conversion to Time
 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.
-
Jul 19th, 2006, 12:15 AM
#3
Thread Starter
Lively Member
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")
------------------------------------------------
-
Jul 19th, 2006, 02:24 AM
#4
Re: Conversion to Time
This works properly (as far as I can see)
VB Code:
Option Explicit
Private Sub txtEnter_Change()
Dim ValText As Single
ValText = Val(txtEnter.Text)
lblTimeResult.Caption = Format$(ValText / 24, "h:nn:ss")
End Sub
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Jul 19th, 2006, 02:48 AM
#5
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:
Dim dtmTime As Date
dtmTime = Val(txtEnter.Text) / 24
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).
-
Jul 19th, 2006, 02:56 AM
#6
Re: Conversion to Time
 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.
-
Jul 19th, 2006, 06:28 AM
#7
Thread Starter
Lively Member
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)
-
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.
-
Jul 19th, 2006, 08:51 AM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|