Results 1 to 15 of 15

Thread: algorithm Help Needed for 12hour clock

  1. #1

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Exclamation algorithm Help Needed for 12hour clock

    Code:
    If lblHour.Text = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
    msgbox("Alarm worked!")
    End If
    I had a 24 hour clock in which you can imagine the algorithm could be easily done by using two drop down boxes with values inside of it.

    Hour: 1-24
    Minute: 00-59

    The labels contain what the user wants as the Alarm Set Time

    However now I am wanting to make this into a 12 hour alarm clock, and I need some help on doing this.


    I have now got 3 drop down boxes
    Hour: 1-11
    Minute: 00:59
    AmPm: Choose Am or Pm


    I am not looking for the code, I am just looking an algorithm to go along with so I can do this, I have spent all day trying to work what I could do, and I just can't do it... Thanks Guys!!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: algorithm Help Needed for 12hour clock

    Show us some code. I personally don't see why you would have had more trouble with this when you were succesful with the other.

    Also, though you didn't ask for anything about it, consider radio buttons for the AM vs PM. A combobox requires two selections, while the radio buttons require just one.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock

    The code I showed you above was all I had...


  4. #4

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock


  5. #5

  6. #6

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock

    Code:
            If cbAmPm.Text = "AM" Then
                If lblHour.Text = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
            ElseIf cbAmPm.Text = "PM" Then
                If lblHour.Text + 12 = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
    Is this what you mean?

  7. #7

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock

    Code:
            If cbAmPm.Text = "AM" Then
                If lblHour.Text = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
            ElseIf cbAmPm.Text = "PM" Then
                If lblHour.Text + 12 = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
            End If
    This works!! except for 12am and 12pm, this is really bugging me now lol... any ideas to fix?

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: algorithm Help Needed for 12hour clock

    The problem seems to be that time actually goes from 0-11, with 12 being used only because people don't really want to use 0. Unfortunately, this means that you have to deal with 12 as a special case. I don't see any good way around checking if lblHour.Text is 12, and setting it to 0 if it is. Of course, setting it to 0 might be a bit odd, since you don't have Option Strict ON, but you can still do it.

    I'm a bit surprised that the code is working as is. Option Strict ON would give you an error when you are implicitly converting a string to an integer for the addition, but what surprises me is that the compiler is smart enough to convert the text to an integer and interpret the + operator as addition instead of leaving it as text and interpreting the + operator as concatenation.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock

    yeah when i was typing it, i was waiting for an error but it did, and i do have option strict on too:S for another bit of code i have...

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: algorithm Help Needed for 12hour clock

    ??? Is this 2008? That should have an error in 2005, since you are adding a string to an integer, but 2008, which I have not used, has type inference, which might let the compiler infer the type from the equals sign.
    My usual boring signature: Nothing

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: algorithm Help Needed for 12hour clock

    You could just use a DateTimePicker instead of ComboBoxes. That way you simply get its Value.TimeOfDay property and you're done.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: algorithm Help Needed for 12hour clock

    does this work out for you?

    Code:
        Public Function ConvertTo24Hour(ByVal hour As Integer, ByVal AM As Boolean) As Integer
    
    
            If AM Then
                Return hour Mod 12
            Else
                Return (hour Mod 12) + 12
            End If
    
    
        End Function

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: algorithm Help Needed for 12hour clock

    fyi - when using 24 hour clocks i don't think there is actually an hour 24. the sequence would be
    23:58
    23:59
    00:00
    00:01

    and this If lblHour.Text = TimeOfDay.Hour, in 2008 with strict on causes an error.
    Last edited by dbasnett; Dec 17th, 2008 at 10:17 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Lively Member RedGunner's Avatar
    Join Date
    Oct 2008
    Location
    Ireland
    Posts
    67

    Re: algorithm Help Needed for 12hour clock

    Yeah exactly! So 24:00 would be 0am although the program won't know this.

    I created this,

    Code:
            If cbAmPm.Text = "AM" Then
                If lblHour.Text = 12 Then
                    tempX = 0
                    If tempX = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                        startalarm()
                    End If
                ElseIf lblHour.Text = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
            End If
            If cbAmPm.Text = "PM" Then
                If lblHour.Text = 12 Then
                    tempY = 12
                    If tempY = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                        startalarm()
                    End If
                ElseIf lblHour.Text = TimeOfDay.Hour And lblMinute.Text = TimeOfDay.Minute And TimeOfDay.Second = 0 Then
                    startalarm()
                End If
            End If
    Great News! It worked and I tested it strictly with every possible scenario... Works a Charm!! thanks for the tip on +12

  15. #15
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    Re: algorithm Help Needed for 12hour clock

    I think the code snippet I wrote does that unless I misunderstood the problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width