无法比较DateTimes

时间:2016-05-16 04:46:21

标签: c# windows forms datetime

我正在尝试创建一个很酷的小程序,所以当它是某个特定日期时,会更改文本框或标签说出来的东西!

if (DateTime.Now.Equals(5/15/2016))
{
    textBox1.Text = "Kameron it's your f***ing birthday!";
    label1.Text = "It's your birthday!";
}

2 个答案:

答案 0 :(得分:1)

您可以将其转换为DateTime并比较Date

DateTime.Now部分
if (DateTime.Now.Date.Equals(new DateTime(2016,5,15)))
{
      textBox1.Text = "Kameron it's your f***ing birthday!";
      label1.Text = "It's your birthday!";
}

或者(如@rob建议的那样)你可以使用DateTime.Today进行比较。

if( DateTime.Today.Equals(new DateTime(2016,5,15)))
{
   ....
}

答案 1 :(得分:0)

试试这个

if (DateTime.Now.ToShortDateString().Equals("5/16/2016"))
{
     textBox1.Text = "Kameron it's your f***ing birthday!";
     label1.Text = "It's your birthday!";
}