Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to compare the equality of two DateTime's in C#... but only to a certain precision?

Tags:

date

c#

datetime

I have two datetimes, one from a timestamp, and another I'm generating in code. I need to test their equality, and would love to do it without too many expressions. Here's an example of my two dates:

DateTime expireTimeStampUTC = 
    DateTime.Parse(UTCValueFromDatabase));
DateTime expectedExpireTime = 
    DateTime.UtcNow.AddHours(NumberOfExpectedHoursInConfig);

This is too high precision of a test:

if (expireTimeStampUTC.Equals(expectedExpireTime)){}

I don't care if they're exact to the second, just the hour.

Could it possibly be the best solution to do something compound like this:

if (expireTimeStampUTC.Date.Equals(expectedExpireTime.Date))
{
    if (!expireTimeStampUTC.Hour.Equals(expectedExpireTime.Hour))
    {
        pass = false;
    }
}

I'm not the most experienced with C#... is there some elegent way to do this?

like image 620
danieltalsky Avatar asked Mar 04 '09 23:03

danieltalsky


People also ask

How can I compare two dates in C?

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).

How do you compare only dates?

CompareTo(DateOnly) Compares the value of this instance to a specified DateOnly value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateOnly value.


2 Answers

If the problem you are having is because of them is a database type, you could convert to that type and compare there, we lose/gain about a millisecond converting to SQLDateTimes on roughly 1/3 of DB saves.

If not, compare the unit you actually care about:

DateTime dt1 = DateTime.UtcNow;
DateTime dt2 = DateTime.UtcNow.AddMinutes(59); // or 1 or 61 for test values;

// if the dates are in the same hour (12:10 == 12:50, 1:58 != 2:02)
if(dt1.Hour == dt2.Hour) // result

or if you care that they are within an hour time span

// if the dates are within one hour of each other (1:58 == 2:02, 3:30 != 4:45)
if((dt1 - dt2).Duration() < TimeSpan.FromHours(1)) // result

Here subtracting dates generates a time span, the duration is the 'absolute value' and then we create the limit explicitly from a unit we care about (FromHours) and compare.

The last line is as clean as I can think to do equality within a particular time span.

like image 193
3 revs, 2 users 97% Avatar answered Oct 25 '22 02:10

3 revs, 2 users 97%


How about finding the difference between the two hours, and seeing if it's below a certain threshold (say 3600 seconds for an hour)?

var diff = expireTimeStamp.Subtract(expectedExpireTime).TotalSeconds;
pass = Math.Abs(diff) < 3600;
like image 24
Matt Hamilton Avatar answered Oct 25 '22 01:10

Matt Hamilton