user1112150
user1112150

Reputation: 13

Linq To SQL - compare two dates in two different formats

I'm trying to compare two dates that are in two different formats:

       var messages = (from m in db.ChatMessages
                        where m.RoomID == roomID &&
                        m.MessageID > messageID &&
                        m.MessageTime > timeUserJoined.AddSeconds(1)
                        orderby m.MessageTime ascending
                        select new { m.MessageID, m.Text, m.User.username, m.MessageTime, m.Color });

My problem is that my Database tables stored DateTime fields in the US format i.e. 12/24/2011 1:35:11 PM. So in the query above, the line m.MessageTime > timeUserJoined.AddSeconds(1) might be 12/24/2011 1:35:11 PM > 24/12/2011 13:35:11 PM

How do I get around this - comparing two dates in two different formats and what are the best practices?

At the mment i'm not getting any records back, I think because of these comparison issues?

Many Thanks :)

Upvotes: 1

Views: 822

Answers (2)

Immersive
Immersive

Reputation: 1704

First, I believe you should be using DateTime.CompareTo() to compare your timestamps.

Second, how are you consuming the messages collection? Are you aware that it's a collection of anonymous, untyped objects?

(I'd comment, but I don't have the rep)

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062790

You state they are DateTime fields... Then good news; DateTime in .NET and TSQL does not have any format - it is just a number. Any particular format you are seeing exists only in the imagination of your IDE or other tools (SSMS etc).

As long as it is DateTime you won't have a problem here.

Upvotes: 2

Related Questions