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

Medium Priority
332 Views
Last Modified: 2013-11-26
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
 
            DropDownList1.Items.Add(s)
 
            min = min + 15
            If (min = 60) Then
                min = 0
                hr = hr + 1
            End If
        Next

Open in new window

Comment
Watch Question

Commented:
Hi,

How about this:

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
 
            DropDownList1.Items.Add(s)
 
            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.

Ask the Experts
Larry Bristersr. Developer

Author

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()
		AddTimeToDropdown(12, 12, " am")
		AddTimeToDropdown(1, 11, " am")
		AddTimeToDropdown(12, 12, " pm")
		AddTimeToDropdown(1, 11, " pm")
	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
				DropDownList1.Items.Add(i.ToString() & minutes(j) & ampm)
			Next j
		Next i
	End Sub

Open in new window

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()
                AddTimeToDropdown(12, 12, " am")
                AddTimeToDropdown(1, 11, " am")
                AddTimeToDropdown(12, 12, " pm")
                AddTimeToDropdown(1, 11, " pm")
        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
                                DropDownList1.Items.Add(i.ToString() & minutes(j) & ampm)
                        Next j
                Next i
        End Sub

Open in new window

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


Larry Bristersr. Developer

Author

Commented:
Pivar,
  You stuck with me from the start...thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.