我想知道一个账户是否在30天内到期.我是否正确使用了DateTime Compare?
if (DateTime.Compare(expiryDate, now) < 30)
{
matchFound = true;
}
我想知道一个账户是否在30天内到期.我是否正确使用了DateTime Compare?
if (DateTime.Compare(expiryDate, now) < 30)
{
matchFound = true;
}
我是否正确使用了日期时间比较?
第Compare
条只提供两个日期的相对位置信息:更少、相等或更大.你想要的是这样的:
if ((expiryDate - DateTime.Now).TotalDays < 30)
matchFound = true;
这会减go 两个DateTime
.结果是具有TotalDays
属性的TimeSpan
对象.
此外,条件可以直接写为:
matchFound = (expiryDate - DateTime.Now).TotalDays < 30;
不需要if
个.