Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

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 ?
Avatar of rspahitz
rspahitz
Flag of United States of America image

How about something like this:

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

Result: 01:00 PM
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")
SOLUTION
Avatar of n2fc
n2fc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isnoend2001

ASKER

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 ?
Thanks n2fc don't have much homework at 67 yrs old
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
Thanks rspahitz, but the time  is part of a filename with no colon eg;
"dy0730.rtf"
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
Sorry, but I have had several solutions deleted recently by the "EE Gestapo!"
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window


You pass the filename into the function and get back a datetime value.
Example:
fname="dy1730.rtf"
debug.print Q_28755419(fname)

Open in new window

Thanks rspahitz
have to check the format date picker produces and add am or pm
Need to add
dim AMPM as string
if val(mytime) > 1159 then
AmPm = "PM"
else
ampm = "AM"
end if
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.
Thanks