|
-
Dec 16th, 2008, 04:19 PM
#1
Thread Starter
Lively Member
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!!
-
Dec 16th, 2008, 04:28 PM
#2
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
 
-
Dec 16th, 2008, 04:36 PM
#3
Thread Starter
Lively Member
Re: algorithm Help Needed for 12hour clock
The code I showed you above was all I had...
-
Dec 16th, 2008, 04:36 PM
#4
Thread Starter
Lively Member
Re: algorithm Help Needed for 12hour clock
-
Dec 16th, 2008, 04:41 PM
#5
Re: algorithm Help Needed for 12hour clock
Just add 12 hours when PM is selected?
-
Dec 16th, 2008, 05:01 PM
#6
Thread Starter
Lively Member
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?
-
Dec 16th, 2008, 05:34 PM
#7
Thread Starter
Lively Member
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?
-
Dec 16th, 2008, 06:08 PM
#8
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
 
-
Dec 16th, 2008, 07:31 PM
#9
Thread Starter
Lively Member
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...
-
Dec 16th, 2008, 07:46 PM
#10
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
 
-
Dec 16th, 2008, 09:32 PM
#11
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.
-
Dec 16th, 2008, 10:15 PM
#12
Hyperactive Member
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
-
Dec 17th, 2008, 10:14 AM
#13
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.
-
Dec 17th, 2008, 10:28 AM
#14
Thread Starter
Lively Member
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
-
Dec 17th, 2008, 10:31 AM
#15
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|