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.