Looping the time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    Looping the time

    Dear programmers;

    I want to shorten my codes in adding a time on a combo box with 30 mins time interval

    My sample code

    Code:
    Combo1.Additem "7:30 AM"
    Combo1.Additem "8:00 AM"
    Combo1.Additem "8:30 AM"
    Combo1.Additem "9:00 AM"
    Combo1.Additem "9:30 AM"
    Combo1.Additem "9:00 AM"
    Combo1.Additem "9:30 AM"
    Combo1.Additem "10:00 AM"
    Combo1.Additem "10:30 AM"
    Combo1.Additem "11:00 AM"
    Combo1.Additem "11:30 AM"
    Combo1.Additem "12:00 PM"
    Combo1.Additem "12:30 PM"
    Combo1.Additem "1:00 PM"
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    I'd suggest you create a variable of type Date. Set it to the start time (in this case, 07:30 I suppose). Then do a loop using the DateAdd() function to add 30 minutes, until it reaches the finish time. Each time around the loop, use the Format() function to get the string you want, and add it to the list.

    Note, this applies to VB6 and may need modification if using a later version.

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Originally posted by lotus18
        I did not try it but I just want to say thank you for your reply. If you won't mind, can you please send me the codes :)
        Further don't ask for code before trying yourself what was suggested to you.

        Comment

        • lotus18
          Contributor
          • Nov 2007
          • 865

          #5
          Sorry guys... I tried and I've spend so much time but still i can't make it...
          Can you please tell me what's wrong with these codes?


          [Code=vb]Dim d As Date
          d="#7:00 AM#"
          Do Until d = #8:00 PM#
          Combo1.AddItem DateAdd("n", 30, d)
          Loop
          [/Code]
          Last edited by Killer42; Nov 11 '07, 10:34 PM.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by lotus18
            Sorry guys... I tried and I've spend so much time but still i can't make it...
            Can you please tell me what's wrong with these codes?
            Sure. You've got unnecessary quotes on line 2, and you're not incrementing the value of d as you go around the loop (which means it will go forever).

            Also, I suspect you will want to use the Format() function to format the value that you place in the combobox. But that can wait until you get the loop working.

            Comment

            • lotus18
              Contributor
              • Nov 2007
              • 865

              #7
              hello killer42. This is my modification.

              [CODE=vb]Dim i As Long
              Dim d As Date
              Dim e As Date

              d=#7:00:00 AM#

              Do
              e=DateAdd("n", i, d)
              Combo1.AddItem e
              i=i+30 'add 30 mins interval
              Loop Until i=810 '8:00:00 PM[/CODE]

              These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by lotus18
                hello killer42. Is this one correct?
                It's looking pretty good. But why not just reuse d instead of creating another variable e? And I wouldn't increment i; you're going to be incrementing your time by half an hour longer each time. Just leave i as it is, and loop until d hits the finishing time.

                As for Format(), hit the books. :) The documentation describes it pretty thoroughly.

                Comment

                • lotus18
                  Contributor
                  • Nov 2007
                  • 865

                  #9
                  Thanks for your immediate reply, killer42 :)

                  Comment

                  • jamesd0142
                    Contributor
                    • Sep 2007
                    • 471

                    #10
                    if it help i came up with this...

                    [code=vbnet]Public Class Form1
                    Dim start1 As String = "00:00"
                    Dim end1 As String = "12:00"
                    Dim i As Integer
                    Dim a As String
                    Dim start2 As String


                    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

                    box.Items.Clear ()

                    box.Items.Add(s tart1)
                    'For i = 1 To Len(start1)
                    ' a = Mid(start1, i, 1)
                    ' If a >= "0" Or a <= "9" Then
                    ' start2 = start2 + a
                    ' End If
                    'Next
                    'start1 = start2

                    'take out :

                    While start1 < end1
                    start2 = ""
                    For i = 1 To Len(start1)
                    a = Mid(start1, i, 1)
                    If a = "0" Or a = "1" Or a = "2" Or a = "3" Or a = "4" Or a = "5" Or a = "6" Or a = "7" Or a = "8" Or a = "9" Then
                    start2 = start2 + a
                    End If
                    Next
                    start1 = start2

                    'Dim convert As String
                    a = Mid(start1, 3, 2)
                    If a = "30" Then
                    start1 = start1 - 30
                    start1 = start1 + 100
                    Else
                    start1 = start1 + 30
                    End If

                    'convert here
                    If Len(start1) = 3 Then
                    start1 = "0" + Mid(start1, 1)
                    End If
                    If Len(start1) = 2 Then
                    start1 = "00" + Mid(start1, 1)
                    End If

                    start1 = Mid(start1, 1, 2) & ":" & Mid(start1, 3, 2)
                    box.Items.Add(s tart1)

                    End While

                    End Sub
                    End Class
                    [/code]

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by jamesd0142
                      if it help i came up with this...
                      Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.

                      Comment

                      • lotus18
                        Contributor
                        • Nov 2007
                        • 865

                        #12
                        Hello James

                        I'm looking for a vb 6.0 code, but thanks for your reply. :)

                        Comment

                        • jamesd0142
                          Contributor
                          • Sep 2007
                          • 471

                          #13
                          Originally posted by Killer42
                          Thanks for that, James. Does it work? I have to admit, it seems like a lot of extra coding for quite a simple task.
                          haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)

                          Comment

                          • lotus18
                            Contributor
                            • Nov 2007
                            • 865

                            #14
                            Originally posted by jamesd0142
                            haha killer you should know by now... i don't write 3 lines of code when i can write 53 lines ;)
                            I think in solving a problem like this we should apply the KISS principle.

                            Comment

                            • QVeen72
                              Recognized Expert Top Contributor
                              • Oct 2006
                              • 1445

                              #15
                              Originally posted by lotus18
                              hello killer42. This is my modification.

                              [CODE=vb]Dim i As Long
                              Dim d As Date
                              Dim e As Date

                              d=#7:00:00 AM#

                              Do
                              e=DateAdd("n", i, d)
                              Combo1.AddItem e
                              i=i+30 'add 30 mins interval
                              Loop Until i=810 '8:00:00 PM[/CODE]

                              These codes work but I can't really make it how to add 30 mins interval on variable "e". It goes looping and looping as you've said. By the way, regarding about the Format function I can't add AM/PM at the end and also the time format is 24-hour. How to change it to 12-hour format?
                              Hi,

                              Try this :
                              [code=vb]
                              Combo1.Clear
                              Dim t As Date
                              Dim i As Integer
                              t = CDate("07:00:00 ")
                              Do
                              t = DateAdd("n", 30, t)
                              Combo1.AddItem Format(t, "hh:mm AM/PM")
                              i = i + 1
                              Loop Until i >= 26

                              [/code]

                              Regards
                              Veena

                              Comment

                              Working...