0

I am in the process of making an alarm clock app in C# via Windows Forms. So far, I have this non-working section of code for my time checker.

string alarm = this.dateTimePicker1.Text;

if (DateTime.Now.ToString() == alarm)
{
    MessageBox.Show("Alarm");
}

The DateTimePicker is configured for hours, minutes, and seconds. The above code is not displaying the message box at all. Where is the problem in the snippet?

Also: are there any more efficient ways of making an alarm clock

Gecko
  • 1,333
  • 1
  • 14
  • 26
  • NineBerry, there is no need for more code. The code is enough to see the problem. – NikxDa Dec 06 '16 at 20:41
  • You could, when the user enters a time start a Timer like so http://stackoverflow.com/a/7970754/2608451 – Gecko Dec 06 '16 at 20:45

4 Answers4

2

Since it is an alarm clock, you'll want to do the compare including the time. That said, compare the .Ticks values of your DateTime objects:

if(DateTime.Now.Ticks >= this.DateTimePicker1.Value.Ticks)
{
    // Sound the alarm
}

Depending on how the DateTimePicker you are using works, you may need to check the Value for null before comparing.

Kevin
  • 1,462
  • 9
  • 9
  • I don't see why you need to use Ticks, this should be enough: `DateTime.Now > this.DateTimePicker1.Value` – smead Dec 06 '16 at 20:52
  • Since Ticks is the base value for DateTime objects, it is a little bit more efficient, but your way works too and the difference would be negligible in the OP's application. – Kevin Dec 06 '16 at 21:05
2

Try this to get you going:

Need to compare Hour and minutes.

private void CheckTime()
{
    clock.Text = DateTime.Now.ToString("hh:mm:ss");
    date.Text = DateTime.Now.ToLongDateString();
    DateTime alarm = this.dateTimePicker1.Value;
    DateTime currentTime = DateTime.Now;
    if (alarm.Hour == currentTime.Hour && alarm.Minute == currentTime.Minute)
    {
       timer1.Enabled = false;
       MessageBox.Show("Alarm");

    }
}
Richard
  • 874
  • 2
  • 9
  • 23
  • Really good, stops the timer, preventing tons of message boxes taking over my screen. –  Dec 06 '16 at 21:22
  • 1
    This is a very succinct answer, but there's no explanation of why it works. @trickrider2002, this answer works because DateTime.Now is going to give you a value that includes seconds (and possibly milliseconds depending on how it is used). Its equivalent to comparing a couple of floating point values. You need to round them to some values that can be meaningfully compared. Converting to hours and minutes does that. – dviljoen Dec 06 '16 at 21:40
0

Modify the below code as needed. I find comparing strings to be a poor way to go about this.

DateTime alarm = this.dateTimePicker1.Value.Date;
if (DateTime.Compare(DateTime.Now.Date, alarm) == 0) { 
     MessageBow.Show("Alarm"); }
peterpep
  • 314
  • 3
  • 11
  • 2
    When using DateTime.Compare do not forget that the datetimes should be constructed in the same timezone. so beware of your Culture and UICulture settings. – Gecko Dec 06 '16 at 20:50
  • @GeckoIT very true! there is a way to specify the culture and UIculture settings. I'm not gonna write the exact code he needs but provide a way for said person to figure it out (i think thats how people best learn). but you made a great point, definitely something to be wary of – peterpep Dec 06 '16 at 20:54
  • i think you want `> 0`, not `== 0` – smead Dec 06 '16 at 21:03
  • "Zero t1 is the same as t2." It depends on how hes using his method. This will check if the two are the same. not if datetime.now.date is after alarm ( > 0 ) – peterpep Dec 06 '16 at 21:05
  • 2
    It checks down to the nearest tick (100 nanoseconds), so this will almost always miss the alarm. – smead Dec 06 '16 at 21:12
  • Then you are correct @smead that >= 0 should be the condition in the if statement – peterpep Dec 06 '16 at 21:14
0

Problem is that you are looking for the exact match. Instead you should be looking if the alarm time has passed (so ">="). Also, the string comparison is not going to work for date time. You need to compare date/times:

        string alarm = this.dateTimePicker1.Value;
        if (DateTime.Now() >= alarm)
        {
            MessageBox.Show("Alarm");
        } 
under
  • 2,519
  • 1
  • 21
  • 40