DateTime.Compare如何检查日期不到30天? [英] DateTime.Compare how to check if a date is less than 30 days old?
问题描述
我想工作,如果一个帐户在不到30天到期。我使用的DateTime比较正确?
如果(DateTime.Compare(expiryDate,现)小于30)
{
matchFound = TRUE;
}
我使用的DateTime比较正确?
没有。 比较
仅能提供约两个日期的相对位置的信息:小于,等于或大于。你需要的是这样的:
IF((expiryDate - DateTime.Now).TotalDays小于30)
matchFound = TRUE;
本减去两个的DateTime
秒。其结果是一个<一个href="http://msdn.microsoft.com/en-us/library/system.timespan%28VS.80%29.aspx"><$c$c>TimeSpan$c$c>对象,它有一个 TotalDays
属性。
此外,有条件可直接写为:
matchFound =(expiryDate - DateTime.Now).TotalDays&LT; 30;
没有如果
必要的。
I'm trying to work out if an account expires in less than 30 days. Am I using DateTime Compare correctly?
if (DateTime.Compare(expiryDate, now) < 30)
{
matchFound = true;
}
Am I using DateTime Compare correctly?
No. Compare
only offers information about the relative position of two dates: less, equal or greater. What you want is something like this:
if ((expiryDate - DateTime.Now).TotalDays < 30)
matchFound = true;
This subtracts two DateTime
s. The result is a TimeSpan
object which has a TotalDays
property.
Additionally, the conditional can be written directly as:
matchFound = (expiryDate - DateTime.Now).TotalDays < 30;
No if
needed.
这篇关于DateTime.Compare如何检查日期不到30天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!