2

I'm making a little alarm clock as a project to practice as I'm just a beginner.

I got 2 textboxes in which the user can put in the hours and minutes at which he wants the alarm to go off.

How do i check if the alarm time provided by the user is the same as the time on his system / pc?

grabthefish
  • 962
  • 14
  • 30
  • You may see this thread for the solution http://stackoverflow.com/questions/1493203/alarm-clock-application-in-net – Habib Jul 03 '12 at 11:50

5 Answers5

1

Use

int hours = System.DateTime.Now.Hour;
int minutes = System.DateTime.Now.Minute;

if(Convert.Toint32(txtHours.Text) == hours && Convert.Toint32(txtMinutes.Text) == minutes)
{
  // same time 
}
else
{
  // time not same
}
Talha
  • 18,898
  • 8
  • 49
  • 66
1

Here is a litle sample to get you on your way

    int myMinute = 5;
    int myHour = 19;

    if (DateTime.Now.Minute == myMinute && DateTime.Now.Hour == myHour)
    { 
      }
JohnnBlade
  • 4,261
  • 1
  • 21
  • 22
1

All above answers are helpful but you should make in practice to use TimeSpan for comparing date or time.

int hour=5;
int min=20;
int sec=0;
TimeSpan time = new TimeSpan(hour, min, sec);
TimeSpan now = DateTime.Now.TimeOfDay;
// see if start comes before end
if (time == now)
{
    //put your condition
}

Please see this url for more info.

How to check if DateTime.Now is between two given DateTimes for time part only?

Community
  • 1
  • 1
Rahul Jha
  • 874
  • 1
  • 11
  • 28
0
if(Convert.ToInt32(tbHours.Text) == DateTime.Now.Hours 
&& Convert.ToInt32(tbMinutes.Text) == DateTime.Now.Minutes)
    {
        //set off alarm
    }
dtsg
  • 4,408
  • 4
  • 30
  • 40
0
var current = DateTime.Now;
if (current.Hour == 9 && current.Minute == 0) {
    //It is 9:00 now
}
penartur
  • 9,792
  • 5
  • 39
  • 50