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?

4

5 回答 5

1

以上所有答案都很有帮助,但您应该在实践中使用 TimeSpan 来比较日期或时间。

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
}

请参阅此网址了解更多信息。

如何检查 DateTime.Now 是否仅在时间部分的两个给定 DateTimes 之间?

于 2015-01-07T07:40:54.350 回答
1

利用

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
}
于 2012-07-03T11:44:46.353 回答
1

这是一个让您上路的小样本

    int myMinute = 5;
    int myHour = 19;

    if (DateTime.Now.Minute == myMinute && DateTime.Now.Hour == myHour)
    { 
      }
于 2012-07-03T11:44:52.167 回答
0
if(Convert.ToInt32(tbHours.Text) == DateTime.Now.Hours 
&& Convert.ToInt32(tbMinutes.Text) == DateTime.Now.Minutes)
    {
        //set off alarm
    }
于 2012-07-03T11:44:24.463 回答
0
var current = DateTime.Now;
if (current.Hour == 9 && current.Minute == 0) {
    //It is 9:00 now
}
于 2012-07-03T11:44:53.743 回答