We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Minute and Hour Dropdown

on
Medium Priority
332 Views
I have a minute and hour dropdown box that works but I'm gettiing a lot of static because it's in "military" time.

In my attached snippet I added the am and pm.  How can I change this to get 1:00 pm instead of 13:00 pm?

``````Dim i As Integer
Dim hr, min As Integer
Dim s As String
hr = 0
min = 0
For i = 0 To 96
If (min = 0 And hr < 10) Then
s = "0" & hr & ":0" & min
ElseIf (min = 0) Then
s = hr & ":0" & min

ElseIf (hr < 10) Then
s = "0" & hr & ":" & min
Else
s = hr & ":" & min
End If
If Left(s, 1) = "0" Or Left(s, 2) = "10" Or Left(s, 2) = "11" Then
s = s & " am"
Else
s = s & " pm"
End If

min = min + 15
If (min = 60) Then
min = 0
hr = hr + 1
End If
Next
``````
Comment
Watch Question

## View Solutions Only

Commented:
Hi,

Dim i As Integer
Dim hr,hr2, min As Integer
Dim s As String
hr = 0
min = 0
For i = 0 To 96
If (hr > 12) Then
hr2 = hr - 12
Else
hr2 = hr
End

If (min < 10 And hr2 < 10) Then
s = "0" & hr2 & ":0" & min
ElseIf (min < 10) Then
s = hr2 & ":0" & min
ElseIf (hr2 < 10) Then
s = "0" & hr2 & ":" & min
Else
s = hr2 & ":" & min
End If
If (hr > 12) Then
s = s & " pm"
Else
s = s & " am"
End If

min = min + 15
If (min = 60) Then
min = 0
hr = hr + 1
End If
Next

Not the solution you were looking for? Getting a personalized solution is easy.

sr. Developer

Commented:
pivar:
I can't have a "01:00" pm...etc

That has to be 1:00 pm...etc

Commented:
The following is a slightly more condensed code sample that will replicate what you are attempting.

Also, might I suggest adding hour, minute, and AM/PM combo boxes (or radio buttons for the AM/PM)?  It will save you some hassle and it is just as clear.  Hope that helps.

``````	Public Sub Foo()
End Sub

Public Shared Sub AddTimeToDropdown(ByVal fromCount As Integer, ByVal toCount As Integer, ByVal ampm As String)
Dim minutes(3) As String
minutes(0) = ":00"
minutes(1) = ":15"
minutes(2) = ":30"
minutes(3) = ":45"

For i As Integer = fromCount To toCount
For j As Integer = 0 To 3
Next j
Next i
End Sub
``````

Commented:
Whoops, you can remove the "Shared" from the AddTimeToDropDown method.  I was testing the code in a console app and forgot to remove that.  Corrected code is as follows.

``````        Public Sub Foo()
End Sub

Public Sub AddTimeToDropdown(ByVal fromCount As Integer, ByVal toCount As Integer, ByVal ampm As String)
Dim minutes(3) As String
minutes(0) = ":00"
minutes(1) = ":15"
minutes(2) = ":30"
minutes(3) = ":45"

For i As Integer = fromCount To toCount
For j As Integer = 0 To 3
Next j
Next i
End Sub
``````
Commented:
Change
If (min < 10 And hr2 < 10) Then
s = "0" & hr2 & ":0" & min
ElseIf (min < 10) Then
s = hr2 & ":0" & min
ElseIf (hr2 < 10) Then
s = "0" & hr2 & ":" & min
Else
s = hr2 & ":" & min
End If
to
If (min < 10) Then
s = hr2 & ":0" & min
Else
s = hr2 & ":" & min
End If

sr. Developer

Commented:
Pivar,
You stuck with me from the start...thanks.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile