问题
I have EndDate
column in my table which accepts records like this
2014-10-29 00:00:00.000
Now I want to compare this DateTime
with present date . For that , I did like this :-
var today=DateTime.Now;
var LstEvents = (from t in _context.Tournaments
where t.SiteId == siteId && t.EndDate != null
&& t.EndDate < today
select t).ToList();
But t.EndDate < today
is not returning results as I wanted . I did Add Watch for today and checked the value and it's format was like
today {11/3/2014 8:12:00 PM} System.DateTime
Therefore my dates are unable to be compared . Any help would be much appreciated
Thanks !!
回答1:
They are comparable. What you see is the string representation of your date that takes a format and a culture into account.
If you're seeing this from the debugger, then it shows the result of ToString()
against your instance, i.e. using the general date and time format specifier ('G') and the culture of the current thread.
(edit: believe this is what he is referring to)
回答2:
The code I have pasted was absolutely correct . I got to know that DateTime
format is irrelevant . Whether it is coming from database or I have used it in C#
code . I was bit confused as I thought that if the DateTime
record in database is set in some format then it cannot be compared with the DateTime
value in C# . Either we have to change the format or do something . Anyway , Thanks guyzz for your help.
Cheers !!
来源:https://stackoverflow.com/questions/26716608/compare-two-different-formats-of-the-datetime