Determine equality of Datetime values with minute precision within LINQ?

Checking whether Math. Abs(diff. TotalMinutes) == 0 won't do it, no - that's checking whether they're exactly the same Are you trying to check whether they have the same minute, or whether they're less than a minute apart?

For the first, use: where RoundToMinute(dateTime1) == RoundToMinute(dateTime2) having declared: 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) If you're using LINQ to SQL, then obviously you can't use local methods, and we'll have to look again EDIT: I'm still very unclear on your question.

If you need them to be exactly the same date/time, it's easy (leaving aside the possible local vs UTC issue): where dateTime1 == dateTime2 However, that begs the question of why you mention "minute precision" in the question title or "using up to a minute precision" in the question body.

Checking whether Math. Abs(diff. TotalMinutes) == 0 won't do it, no - that's checking whether they're exactly the same.

Are you trying to check whether they have the same minute, or whether they're less than a minute apart? For the first, use: where RoundToMinute(dateTime1) == RoundToMinute(dateTime2) having declared: 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).

Jon - I was wary of using RoundToMinute(datetime) as the incoming dates could possibly have seconds on them and I wanted to maintain a minute precision. Although Math. Abs might throw it off as well I guess?

I could(should) strip the seconds off before using the dates of course! But to answer your query, yes I do need them to be exactly the same – PreethaA Aug 12 '09 at 13:54 1 @Preetha: If you need them to be exactly the same, then in what way do you need "minute precision" as per the question title? – Jon Skeet Aug 12 '09 at 13:56 In case you have datetime values coming in, where you cannot be sure of whether or not they have seconds attached, but all you care about it is whether they are exactly the same..down to the minute.

I guess the stripseconds part should be totally seperate and a precede the actual compare of the values..so this would work in that case. – PreethaA Aug 14 '09 at 13:22 There's no concept of DateTime "having seconds attached" or not - they always have a Second value. Maybe that's been set to a meaningful value, and maybe it hasn't - you won't be able to tell.It sounds like my answer probably is what you want, but you need to be clear in your mind exactly what you want to happen first.

Write some unit tests demonstrating the behaviour you want, then see if my code makes your unit tests pass. – Jon Skeet Aug 14 '09 at 13:56.

How about where (Math. Floor(datetime1. Ticks / 600000000) == Math.

Floor(datetime2. Ticks / 600000000))?

Exactly what I thought of when I saw the question – Allen Aug 10 '09 at 18:37 If you mean datetime1. Millisecond, that's not right - the value is always between 0 and 999. – Jon Skeet Aug 10 '09 at 18:42.

Thanks Allen,will do that(still figuring out exactly how the process works! ). Here is the code snippet indicating the answer I had come up with initially: (Math.

Abs(datetime1. Subtract(datetime2). TotalMinutes) == 0) Background: Within my linq query's WHERE clause(hence a one-liner), I need to check if datetime1 and datetime2 are exactly the same.

The datetime values under consideration are stamp dates for other values used in the query.

As edited in my answer, just use dateTime1 == dateTime2. Please update your question to get rid of the "using up to a minute precision" as that isn't the same thing as "exactly the same. " – Jon Skeet Aug 12 '09 at 13:58.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions