Old 05-05-2007   #1 (permalink)
Monster Member
 
ShiFTy's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 2,462
Rep Power: 0
Default VB Help :(

So basically i'm quite clueless at the syntax and structure of VB (declaring stuff, format etc) so i need some guidance. My task is to convert 24 hour time into 12 hour time so it changes from the form #### to ##:##am or pm (e.g. 1350 to 1:50pm)


What i've done so far probably doesn't make sense so please help!

Quote:
Function ConvertTimeTo12(intTime24 As Variant) As String

Dim intMins As Integer
Dim intHours As Integer

intHours = inTime24 \ 100
intMins = inTime24 Mod 100

If intHours < 12 Then
"somehow make it say am" <--- how do i do this? suffix? :S
Else
"somehow make it say pm" <--- same here
End If
They told us to somehow split the number into pairs so thats what i think you have to do. Then maybe if the intHours was greater than 12, then somehow minus 12 so it displays it as for example, 1 instead of 13

Then for the formatting, joining the intHours and intMins and do something to get it into the correct format...
__________________
ShiFTy is offline   Reply With Quote
Old 05-05-2007   #2 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

This is probably a very hacky way of doing this.. but its my 3 minute attempt..
PHP Code:
Function ConvertTimeTo12(intInTime As Long) As String
Dim ret 
As String ' return string
Dim ampm As String ' 
am or pm?

Dim inHour As Long
Dim inMinute 
As Long

inHour 
CInt(CStr(Mid$(CStr(intInTime), 12)))
inMinute CInt(CStr(Mid$(CStr(intInTime), 32)))

If (
inHour 12Then
    inHour 
inHour 12
    ampm 
"pm"
Else
    
ampm "am"
End If

ret ret CStr(CStr(inHour) + ":" CStr(inMinute) + ampm)
ConvertTimeTo12 ret
End 
Function 
Btw:

Stay away from Variant's at all costs. If you start using them for small pieces of code, they'll catch on, and make your programs slower than they already are with VB ..

To use it:
PHP Code:
MsgBox ConvertTimeTo12(1350
chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 05-05-2007   #3 (permalink)
Monster Member
 
ShiFTy's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 2,462
Rep Power: 0
Default

Cheers for that chem! Now i just gotta modify it so its anti-hacky

Will report back later with some progress
__________________
ShiFTy is offline   Reply With Quote
Old 05-05-2007   #4 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Quote:
Originally Posted by ShiFTy View Post
Cheers for that chem! Now i just gotta modify it so its anti-hacky

Will report back later with some progress


You could even use the Date type if you wanted. I just assume when you said "1350" that you meant an actual number.

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 05-05-2007   #5 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

My attempt at that wouldn't be quite as professional but I think it would work.

Anything higher than 1200, change am to pm.

Put a colon after 2 digits.

Check the first 2 digits if it is 13, make it 1, if it is 14, make it 2. With some huge arse pathetic slow case select.

I can do it if you want but its pretty unprofessional.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 05-05-2007   #6 (permalink)
Monster Member
 
ShiFTy's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 2,462
Rep Power: 0
Default

I did mean an actual number, so i still have to put some "exit functions" or something if the number is invalid. i.e. if the last 2 numbers exceed 59 and if the first 2 numbers exceed 23 etc but that seems easy enough to do

We were supposed to able to use this newly created function in the Excel worksheet so when we type =ConvertTimeTo12(A1) it will return the formatted time
__________________
ShiFTy is offline   Reply With Quote
Old 05-05-2007   #7 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

EDIT: Figured I'd give you the whole function instead of the extra fix:
PHP Code:
Function ConvertTimeTo12(intInTime As Long) As String
Dim ret 
As String ' return string
Dim ampm As String ' 
am or pm?

Dim inHour As Long
Dim inMinute 
As Long

inHour 
CInt(CStr(Mid$(CStr(intInTime), 12)))
inMinute CInt(CStr(Mid$(CStr(intInTime), 32)))

If (
inHour 12Then
    inHour 
inHour 12
    ampm 
"pm"
Else
    
ampm "am"
End If

ret ret CStr(CStr(inHour) + ":" CStr(inMinute) + ampm)
ConvertTimeTo12 ret

If (inMinute 59Then
    GoTo BadMinute
End 
If
If (
inHour 23Then
    GoTo BadHour
End 
If
Exit Function
BadMinute:
MsgBox "Incorrect Minute Value"vbCritical"Bad Minute"
ret CStr(intInTime)
ConvertTimeTo12 ret
Exit Function
BadHour:
MsgBox "Incorrect Hour Value"vbCritical"Bad Hour"
ret CStr(intInTime)
ConvertTimeTo12 ret
Exit Function
End Function 


chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-

Last edited by chemicalNova; 05-05-2007 at 02:02 PM..
chemicalNova is offline   Reply With Quote
Old 05-05-2007   #8 (permalink)
Contributing Member
 
badkarma's Avatar
 
Join Date: May 2006
Location: tas
Posts: 616
Rep Power: 3
Default

rofl, thats ugly, glad i dont have to work with vb
badkarma is offline   Reply With Quote
Old 05-05-2007   #9 (permalink)
Monster Member
 
ShiFTy's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 2,462
Rep Power: 0
Default

Haha chem, you're too good for what my course expects


Instead of having popups etc, would this still be right? (syntax wise)
Quote:
If inHour > 23 Then
ConvertInTime24 = "Bad Hour"
End If
If inMinute > 59 Then
ConvertInTime24 = "Bad Minute"
End If
So it just displays the text into the cell?


Also, the format for the time works for everything apart from times that end in 00 in which it would only display 1 zero in the converted time. i.e. 1500 becomes 3:0pm instead of 3:00pm. Is this where formatting comes in place?

I remember the lecturer typing Print.Debug or something and then with some random '&'s everywhere instead of where you placed 'ret'....is this just an alternate method?
__________________
ShiFTy is offline   Reply With Quote
Old 05-05-2007   #10 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Quote:
Originally Posted by ShiFTy View Post
Instead of having popups etc, would this still be right? (syntax wise)

So it just displays the text into the cell?
I thought about that.. but figured, if you're using it in Excel, instead of removing the time entered.. it would just return the original - incorrect - time.
Quote:
Originally Posted by ShiFTy
Also, the format for the time works for everything apart from times that end in 00 in which it would only display 1 zero in the converted time. i.e. 1500 becomes 3:0pm instead of 3:00pm. Is this where formatting comes in place?
The way to fix that is to check if the two righthanded digits are zero's.. something like:
PHP Code:
If (Right$(CStr(intInTime),2) = "00"Then
    ampm 
ampm "0"
End If 
Quote:
Originally Posted by ShiFTy
I remember the lecturer typing Print.Debug or something and then with some random '&'s everywhere instead of where you placed 'ret'....is this just an alternate method?
Debug.Print just prints to the immediate window inside the editor. If you're writing this as a macro in Excel, then I'm not sure where it'll be. In the VB IDE its View >> Immediate Window. The immediate window is just for debugging stuff. So if you wanted to check the value of something while looping..
PHP Code:
For 0 To 50
    Debug
.Print i
Next i 
.. that would print into the immediate window while your program ran.

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 05-05-2007   #11 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

Quote:
Originally Posted by ShiFTy View Post
Haha chem, you're too good for what my course expects


Instead of having popups etc, would this still be right? (syntax wise)

So it just displays the text into the cell?


Also, the format for the time works for everything apart from times that end in 00 in which it would only display 1 zero in the converted time. i.e. 1500 becomes 3:0pm instead of 3:00pm. Is this where formatting comes in place?

I remember the lecturer typing Print.Debug or something and then with some random '&'s everywhere instead of where you placed 'ret'....is this just an alternate method?
Manually format it?

x = "1500"
y = InStr(x, "00")
z = Mid(x,y)

If z > 1 then
`change the output
end if

I tried. >_<

EDIT: too slow.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 05-05-2007   #12 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Quote:
Originally Posted by shirasE_ View Post
y = InStr(x, "00")
z = Mid(x,y)
For future reference shirase

Left() Mid() and Right() return variants.
Left$() Mid$() and Right$() return strings.

If you're working with strings, make sure you use the right functions for better performance

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 05-05-2007   #13 (permalink)
Monster Member
 
shirasE_'s Avatar
 
Join Date: Jan 2007
Location: Melbourne, Australia.
Age: 18
Posts: 3,555
Rep Power: 5
Send a message via ICQ to shirasE_ Send a message via AIM to shirasE_ Send a message via MSN to shirasE_ Send a message via Yahoo to shirasE_ Send a message via Skype™ to shirasE_
Default

Quote:
Originally Posted by chemicalNova View Post
For future reference shirase

Left() Mid() and Right() return variants.
Left$() Mid$() and Right$() return strings.

If you're working with strings, make sure you use the right functions for better performance

chem
Awesome I didn't know about that dollar sign thing. =D You know I'm a failure at least I try. Haha.
__________________
AMD 5000+BE@3.21GHZ//2GB OCZREAPER//ATI HD3870@850/1301
ANTEC 900//RAZER TARANTULA//TT TAI-CHI M2//SS 5HV2 XFI
#GotGames #MelbCSS #s2.au #Reality.Gaming
#TRAiNWRECK melb.lan.net.invite - kto_-[1b]
shirasE_ is offline   Reply With Quote
Old 05-05-2007   #14 (permalink)
Banned
 
Join Date: Jul 2006
Location: Central Coast NSW
Age: 19
Posts: 6
Rep Power: 0
Default

wow i dont understand a thing in here and i did VB for 2 years but i still got full marks for coding and desing. ut go 0 marks for no documentation in my yr12 major =[
Bing is offline   Reply With Quote
Old 05-05-2007   #15 (permalink)
Godlike Member
 
SubNoize's Avatar
 
Join Date: Jul 2006
Location: Sydney
Posts: 8,782
Rep Power: 11
Default

Quote:
Originally Posted by Bing View Post
wow i dont understand a thing in here and i did VB for 2 years but i still got full marks for coding and desing. ut go 0 marks for no documentation in my yr12 major =[
pretty sure a something done in paint woulda been sufficient for this thread.
__________________
««« Qlimax-Crew - #Q-Base - Qlimax-Crew.com »»»
SubNoize is offline   Reply With Quote
Old 05-05-2007   #16 (permalink)