比较如何检查日期是否少于30天?

我正在尝试计算一个帐户是否在30天内过期。我使用的日期时间比较正确吗?

if (DateTime.Compare(expiryDate, now) < 30)


{
matchFound = true;
}
203029 次浏览

No, the Compare function will return either 1, 0, or -1. 0 when the two values are equal, -1 and 1 mean less than and greater than, I believe in that order, but I often mix them up.

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 DateTimes. The result is a TimeSpan object which has a TotalDays property.

Additionally, the conditional can be written directly as:

bool matchFound = (expiryDate - DateTime.Now).TotalDays < 30;

No if needed.

Alternatively, you can avoid naked numbers by using TimeSpan.FromDays:

bool matchFound = (expiryDate - DateTime.Now) < TimeSpan.FromDays(30);

This is slightly more verbose but I generally recommend using the appropriate types, and the appropriate type in this case is a TimeSpan, not an int.

No you are not using it correctly.

See here for details.

DateTime t1 = new DateTime(100);
DateTime t2 = new DateTime(20);


if (DateTime.Compare(t1, t2) >  0) Console.WriteLine("t1 > t2");
if (DateTime.Compare(t1, t2) == 0) Console.WriteLine("t1 == t2");
if (DateTime.Compare(t1, t2) <  0) Console.WriteLine("t1 < t2");

Try this instead

if ( (expiryDate - DateTime.Now ).TotalDays < 30 ) {
matchFound = true;
}

What you want to do is subtract the two DateTimes (expiryDate and DateTime.Now). This will return an object of type TimeSpan. The TimeSpan has a property "Days". Compare that number to 30 for your answer.

No it's not correct, try this :

DateTime expiryDate = DateTime.Now.AddDays(-31);
if (DateTime.Compare(expiryDate, DateTime.Now.AddDays(-30)) < 1)
{
matchFound = true;
}

Compare returns 1, 0, -1 for greater than, equal to, less than, respectively.

You want:

    if (DateTime.Compare(expiryDate, DateTime.Now.AddDays(30)) <= 0)
{
bool matchFound = true;
}

Well I would do it like this instead:

TimeSpan diff = expiryDate - DateTime.Today;
if (diff.Days > 30)
matchFound = true;

Compare only responds with an integer indicating weather the first is earlier, same or later...

should be

matchFound = (expiryDate - DateTime.Now).TotalDays < 30;

note the total days otherwise you'll get werid behaviour

This will give you accurate result :

if ((expiryDate.Date - DateTime.Now.Date).Days < 30)
matchFound = true;

Compare is unnecessary, Days / TotalDays are unnecessary.

All you need is

if (expireDate < DateTime.Now) {
// has expired
} else {
// not expired
}

note this will work if you decide to use minutes or months or even years as your expiry criteria.

Actually none of these answers worked for me. I solved it by doing like this:

  if ((expireDate.Date - DateTime.Now).Days > -30)
{
matchFound = true;
}

When i tried doing this:

matchFound = (expiryDate - DateTime.Now).Days < 30;

Today, 2011-11-14 and my expiryDate was 2011-10-17 i got that matchFound = -28. Instead of 28. So i inversed the last check.

// this isn't set up for good processing.
//I don't know what data set has the expiration
//dates of your accounts.  I assume a list.
// matchfound is a single variablethat returns true if any 1 record is expired.


bool matchFound = false;
DateTime dateOfExpiration = DateTime.Today.AddDays(-30);
List<DateTime> accountExpireDates = new List<DateTime>();
foreach (DateTime date in accountExpireDates)
{
if (DateTime.Compare(dateOfExpiration, date) != -1)
{
matchFound = true;
}
}

Assuming you want to assign false (if applicable) to matchtime, a simpler way of writing it would be..

matchtime = ((expiryDate - DateTime.Now).TotalDays < 30);

You can try to do like this:

var daysPassed = (DateTime.UtcNow - expiryDate).Days;
if (daysPassed > 30)
{
// ...
}