我的代码是这样的,但是我需要验证RemainingDay
是否大于10,然后在剩余的几天中不会显示消息框
if (Convert.ToDateTime(dr[0]) == DateTime.Today)
{
MessageBox.Show("Your trail period is expired. Please purchase this Software.", "SMS");
Application.Exit();
}
else
{
newDate = Convert.ToDateTime(dr[0]);
RemainingDay = newDate.Subtract(TodayDate);
MessageBox.Show("You have " + RemainingDay.Days + " days Left.", "SMS");
this.Hide();
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
我尝试了下面的代码:
if (Convert.ToDateTime(dr[0]) == DateTime.Today)
{
MessageBox.Show("Your trail period is expired. Please purchase this Software.", "SMS");
Application.Exit();
}
else
{
newDate = Convert.ToDateTime(dr[0]);
RemainingDay = newDate.Subtract(TodayDate);
if (RemainingDay <= 10)
{
MessageBox.Show("You have " + RemainingDay.Days + " days Left.", "SMS");
this.Hide();
}
else
{
this.Hide();
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
}
上面的代码给我这个错误:
Operator '>=' cannot be applied to operands of type 'System.DateTime?' and 'int'
请帮我。
最佳答案
您的RemainingDay
变量是 TimeSpan
,它是给定的DateTime
和今天的日期之间的差。您看不到TimeSpan
是否小于数字-这没有任何意义。
您想要得到的是日期之间的天数差,TimeSpan
保存在 .Days
属性中,这将给您(来自MSDN)
the days component of the time interval represented by the current
TimeSpan
structure.
所以用那个代替。
关于c# - 运算符 '>='不能应用于 'System.DateTime?'和 'int'类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809976/