If the problem you are facing because of them is the database type, you can convert it to this type and compare there, we lose / get about a millisecond conversion to SQLDateTimes by about 1/3 of the DB.
If not, compare the block you really need:
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 are interested in what they are in the hour
// 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 interval, duration is an “absolute value”, and then we create a constraint explicitly from the unit we care about (FromHours) and compare.
The last line is as clean as I can think, to make equality for a certain period of time.
Colin dabritz
source share