# convert 24 hour time to AmPm time using vb6

trying  to write a function to convert 24 hour time  to AM?PM time
Public Function Convert24Hour
eg;
1730 = 5:30PM
how to write this ?
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
How about something like this:

format("13:00","medium time")

Result: 01:00 PM
0
Commented:
If you want a bit more control, you can use the letters (h, m, s) rather than the name (medium time):

format("17:30","hh:mm:ss AM/PM")
0
Commented:
As this may be a homework assignment, we may not give solution, only guidance...

Basically, it will be a series of "if" statements to compare ranges...

if the input is "2400" or "0000" then the output is "12:00 AM"...
else if the input is between 0001 and 0059, output is 12:xx AM (where xx is the same as input)...
else if the input is between 100 and 1159 , output is the input + AM
else if the input is between 1200 and 1259, output is the input + PM
else (finally) if the input is between 1300 and 2359, output is (input-1200) + PM
0
Author Commented:
Thanks, i must be doing something wrong i have tried passing as string and date
Private Sub Command3_Click()
Dim mytime As String 'Date
mytime = Convert24Hour("730")
MsgBox mytime

Public Function Convert24Hour(mytime As String)
Convert24Hour = Format(mytime, "medium time")
MsgBox Convert24Hour
End Function

what is wrong this produces 12:00 AM ?
0
Author Commented:
Thanks n2fc don't have much homework at 67 yrs old
0
Commented:
I think the time has to be in a very precise format (including the colon) before the format command works correctly.  Try this:

...
mytime = Convert24Hour("07:30")' leading 0 probably optional

---

It's also good practice for functions to return the preferred data type (As String appended):
Public Function Convert24Hour(mytime As String) As String
0
Author Commented:
Thanks rspahitz, but the time  is part of a filename with no colon eg;
"dy0730.rtf"
0
Author Commented:
sure a lot easier to convert am/pm to 24 hour

Public Function Convert12Hour(mytime As String)
Convert12Hour = Format(mytime, "HHMM")
End Function
guess the only way is to parse
the am/pm
hour
and minute
thought their may be an easier way
0
Commented:
Sorry, but I have had several solutions deleted recently by the "EE Gestapo!"
0
Commented:
If you know that the data is a certain format (3 or 4 digits of text), you could always update it as you go:

Dim rtfTime As String
Dim mytime As String
rtfTime = "730"
mytime = Convert24Hour(Left(rtfTime, Len(rtfTime) - 2) & ":" & Right(rtfTime, 2))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
``````Function Q_28755419(parmFileName) As Date
Static oRE As Object
Dim oSM As Object
If oRE Is Nothing Then
Set oRE = CreateObject("vbscript.regexp")
oRE.Global = False
oRE.Pattern = ".+(\d\d)(\d\d)\."
End If
Set oSM = oRE.Execute(parmFileName)(0).submatches
q = TimeSerial(oSM(0), oSM(1), 0)
End Function
``````

You pass the filename into the function and get back a datetime value.
Example:
``````fname="dy1730.rtf"
debug.print Q_28755419(fname)
``````
0
Author Commented:
Thanks rspahitz
have to check the format date picker produces and add am or pm
dim AMPM as string
if val(mytime) > 1159 then
AmPm = "PM"
else
ampm = "AM"
end if
0
Commented:
The Format function allows you to format numbers, dates et al.
With dates, you can use predefined names ("Medium Time") or custom formats ("HH:MM:SS" or "HH:MM:SS AM/PM" or "HH:MM AM/PM")
Using that, you won't need your AMPM "If" condition.
0
Author Commented:
Thanks
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.