Solved

How to convert military time to standard time?

Posted on 2004-09-11
8
25,330 Views
Last Modified: 2011-08-18
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

Thanks
0
Comment
Question by:rkckjk
  • 3
  • 2
  • 2
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12035581
strStandardTime = Format$(Left$(strMilitaryTime,2) & ":" & Right$(strMilitaryTime,2), "Medium Time")
0
 
LVL 2

Author Comment

by:rkckjk
ID: 12035610
All I get is the following

:


0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12035614
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
    Next
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"
    Else
        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
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12036078
What was the value of  your?

strMilitaryTime.

If it is not in the format of the string that you said that it would be, then the procedure won't work.
0
 
LVL 2

Author Comment

by:rkckjk
ID: 12036216
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.

Sorry
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12036314
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:
http://www.experts-exchange.com/Community_Support/

Be sure to give a link back this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21127517.html

Regards,

Idle_Mind
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12037303
rkckjk,

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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now