2015-07-11 86 views
2

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

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"); 
    } 

如果有办法做到这一点,请帮助我。谢谢。

回答

0

您可以使用TimeOfDay propertyDateTimeTimeSpan值比较;

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"); 
} 
+0

好的。谢谢。我会测试它,看看它回来了。 – Sachin