Equate Datetime Values ​​with Minimal Accuracy in LINQ

I need to compare two datetime values ​​in order to determine equality (exactly the same) using minimal precision. Would this be the best way to do this? My dates can have seconds and milliseconds, but I want to consider only up to minutes.

where (Math.Abs(datetime1.Subtract(datetime2).TotalMinutes) == 0) 
+6
equality c # datetime precision
source share
3 answers

Checking whether Math.Abs(diff.TotalMinutes) == 0 , no - checks to see if they are exactly the same.

Are you trying to check if they have the same minute, or are they less than a minute? For the first use:

 where RoundToMinute(dateTime1) == RoundToMinute(dateTime2) 

declaring:

 public static DateTime RoundToMinute(DateTime time) { return new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0, time.Kind); } 

For the second use:

 where Math.Abs((dateTime1 - dateTime2).TotalMinutes) < 1 

You must consider that you want the result if it is local and one in UTC, by the way ...

Please note that LINQ is not specified here - it is assumed that you are using LINQ for objects. If you use LINQ to SQL, then obviously you cannot use local methods, and we will have to look again ...

EDIT: I am still very unclear in your question. If you need them to be exactly the same date / time, this is easy (not to mention a possible local and UTC problem):

 where dateTime1 == dateTime2 

However, the question arises as to why you mention "minimal accuracy" in the title of the question or "use to the smallest accuracy" in the body of the question.

+11
source share

What about

 where (Math.Floor(datetime1.Ticks / 600000000) == Math.Floor(datetime2.Ticks / 600000000)) 

?

0
source share

Thanks to Allen, let's do it (still figuring out exactly how this process works!). Here is a code snippet indicating the answer I originally came up with:

(Math.Abs ​​(datetime1.Subtract (datetime2) .TotalMinutes) == 0)

Reference Information. In my WHQL WHQL query (hence, single-line) I need to check if datetime1 and datetime2 match. The considered date and time values ​​are stamp dates for other values ​​used in the request.

0
source share

All Articles