• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Minute and Hour Dropdown

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

0
lrbrister
Asked:
lrbrister
  • 2
  • 2
  • 2
2 Solutions
 
pivarCommented:
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
0
 
lrbristerAuthor Commented:
pivar:
  I can't have a "01:00" pm...etc

That has to be 1:00 pm...etc
0
 
jzierseCommented:
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

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
jzierseCommented:
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

0
 
pivarCommented:
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


0
 
lrbristerAuthor Commented:
Pivar,
  You stuck with me from the start...thanks.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now