Compare Two different formats of the DateTime

半城伤御伤魂 提交于 2020-01-06 19:51:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!