I am having problems formatting time in visual basic. I need it to be in 24 hour time without the AM or PM at the end. So if I wanted 1:29pm (12-hour) it would come to be 13:29
I am having problems formatting time in visual basic. I need it to be in 24 hour time without the AM or PM at the end. So if I wanted 1:29pm (12-hour) it would come to be 13:29
What version of VB?
In VB6, you should be able to use Format(YourDate Time, "hh:nn").
well windows died on me, so im running on a 40GB hard drive.
I had (reinstalling now) VB6.
And I recovered all my data succesfully, so I will try this soon.
Thanks!
One more question, if I convert it to 24 hour time, then send it to a string variable, will is still work using the < and > (less than/greater than) statements on it. Cause it works if you just use [text1.text = time] and then in a timer just have like
If text1.text = > 12:32:01 PM then
text2.text = "Lunch is over"
end if
cause thats worked for me so far. The only reason that I need 24 hour time is because VB doesn't regognize if it is AM or PM. So even if its > 12:32:01 AM the code will still run.
I'd say the problem there is just that you're comparing string. So all it's doing is looking at the characters in the string, one by one, not the actual time value. As a string, "2:13 AM" is greater than "12:54 PM" simply because the character "2" has a higher ASCII value than "1". If you convert them to an actual time value of course, it's a different matter.
If you want to compare times you're better off converting back and forth as required. Working with strings that represent things like a date or time is very risky, as the format can change from one PC to the next.
Tip - in many cases it's better to use something like a datetime picker control rather than a textbox. In some cases, I've also used one or two labels to display a time value, with a couple of "spinner" or "updown" controls for the user to adjust the time. If you let people type in a value, they will always find some way to stuff it up.
(In fact I had to debug a case like this just recently, where the user entered a time of "15:3O". Which looks alright, but most certainly isn't. Try copying and pasting it if you don't believe me.)
Also, for your comparison I think you can use something like this...
[CODE=vb]
If CDate(Text1.Tex t) > CDate("12:30:59 ") Then
Text2.Text = "Lunch is over"
End If[/CODE]Of course, ideally you'd (A) validate Text1.Text first to ensure it's a proper time value, and (B) use a constant (or variable) for the end of lunch, not hard-code a string in the test like this and convert it every time.
Comment