C# 在时间段内激活

C# 在时间段内激活,c#,windows,forms,datetime,C#,Windows,Forms,Datetime,我想知道是否有一种方法可以让我在每天的某个时间显示一个消息框。例如 if (DateTime >= 11:59) { messagebox.show("Good Morning"); } else if (DateTime == 12:00 to 16:59) { messagebox.show("Good Afternoon"); } else if (DateTime <= 17:00)

我想知道是否有一种方法可以让我在每天的某个时间显示一个消息框。例如

    if (DateTime >= 11:59)
    {
       messagebox.show("Good Morning");
    }
    else if (DateTime == 12:00 to 16:59)
    {
       messagebox.show("Good Afternoon");
    }
    else if (DateTime <= 17:00)
    {
       messagebox.show("Good Evening");
    }
if(DateTime>=11:59)
{
messagebox.show(“早上好”);
}
否则如果(日期时间==12:00至16:59)
{
messagebox.show(“下午好”);
}
else if(DateTime您可以使用
DateTime
TimeSpan
值进行比较,如:

var dt = DateTime.Now;
if (dt.TimeOfDay <= new TimeSpan(11, 59, 0))
{
   MessageBox.Show("Good Morning");
}
else if (dt.TimeOfDay >= new TimeSpan(12, 0, 0) && dt.TimeOfDay < new TimeSpan(16, 59, 0))
{
   MessageBox.Show("Good Afternoon");
}
else if (dt.TimeOfDay >= new TimeSpan(17, 0, 0))
{
   MessageBox.Show("Good Evening");
}
var dt=DateTime.Now;
如果(dt.TimeOfDay=新时间跨度(12,0,0)和&dt.TimeOfDay<新时间跨度(16,59,0))
{
MessageBox.Show(“下午好”);
}
否则如果(dt.TimeOfDay>=新的时间跨度(17,0,0))
{
MessageBox.Show(“晚上好”);
}

好的。谢谢。我会测试它,看看它会带来什么。