How to convert military time to standard time?

What's an easy way to convert military time to standard time examples:

2115  = 9:15 pm
0051 = 00:51 am
1734 = 5:34 pm
0912 = 9:12 am

Richard KreidlSoftware DeveloperAsked:
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Option Explicit

Private Sub Command1_Click()
    Dim milTimes As Variant
    Dim stdTime As Date
    Dim milTime As String
    Dim curMilTime As Variant
    milTime = "2115,0051,1734,0912"
    milTimes = Split(milTime, ",")
    For Each curMilTime In milTimes
        stdTime = militaryToStandardTime(curMilTime)
        Debug.Print curMilTime & " = " & stdTime
End Sub

Private Function militaryToStandardTime(ByVal militaryTime As String) As Date
    Dim n As Date
    Dim curDateTime As String
    Dim strHour As String
    Dim strMinute As String
    Dim ampm As String
    strHour = Left(militaryTime, 2)
    strMinute = Right(militaryTime, 2)
    If Val(strHour) > 12 Then
        strHour = CStr(Val(strHour) - 12)
        ampm = "PM"
        ampm = "AM"
        If strHour = "00" Then
            strHour = "12"
        End If
    End If
    n = Now()
    curDateTime = Day(n) & "/" & Month(n) & "/" & Year(n) & " " & strHour & ":" & strMinute & " " & ampm
    If IsDate(curDateTime) Then
        militaryToStandardTime = CDate(curDateTime)
    End If
End Function
strStandardTime = Format$(Left$(strMilitaryTime,2) & ":" & Right$(strMilitaryTime,2), "Medium Time")
Richard KreidlSoftware DeveloperAuthor Commented:
All I get is the following


Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

What was the value of  your?


If it is not in the format of the string that you said that it would be, then the procedure won't work.
Richard KreidlSoftware DeveloperAuthor Commented:
Your method would have worked I just tested it wrong. My mistake. I don't know if you can get the points since I already accepted another answer.

Mike TomlinsonMiddle School Assistant TeacherCommented:
The difference betwen our two solutions is that I create a Date out of the military time while, GrahamSkan's method returns a string representing the military time in standard format.

To get the points changed you can post a request here:

Be sure to give a link back this question:



Don't bother to change the points. I just got all the points by mistake on another question. We can't actually do anything with them and anyway, Idle_Mind's solution was far more comprehensive.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.