Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查设定时间是否等于系统时间_C#_Datetime_Clock - Fatal编程技术网

C# 如何检查设定时间是否等于系统时间

C# 如何检查设定时间是否等于系统时间,c#,datetime,clock,C#,Datetime,Clock,我正在制作一个小闹钟作为一个项目来练习,因为我只是一个初学者 我有两个文本框,用户可以在其中输入他希望闹钟响的时间和分钟 如何检查用户提供的报警时间是否与其系统/电脑上的时间相同?使用 if(Convert.ToInt32(tbHours.Text) == DateTime.Now.Hours && Convert.ToInt32(tbMinutes.Text) == DateTime.Now.Minutes) { //set off alarm

我正在制作一个小闹钟作为一个项目来练习,因为我只是一个初学者

我有两个文本框,用户可以在其中输入他希望闹钟响的时间和分钟

如何检查用户提供的报警时间是否与其系统/电脑上的时间相同?

使用

if(Convert.ToInt32(tbHours.Text) == DateTime.Now.Hours 
&& Convert.ToInt32(tbMinutes.Text) == DateTime.Now.Minutes)
    {
        //set off alarm
    }

这是一个小样品,让你上路

    int myMinute = 5;
    int myHour = 19;

    if (DateTime.Now.Minute == myMinute && DateTime.Now.Hour == myHour)
    { 
      }

以上所有答案都很有帮助,但您应该在实践中使用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
}
请查看此url以了解更多信息


您可能会看到此线程的解决方案
var current = DateTime.Now;
if (current.Hour == 9 && current.Minute == 0) {
    //It is 9:00 now
}
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
}