日期时间比较错误 [英] Wrong datetime comparison
本文介绍了日期时间比较错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var db = new XDataContext();
var q = from t in db.Posts select t;
var to = new DateTime(2011, 2, 1).AddMilliseconds(-1);
q = q.Where(n => n.Date <= to);
foreach (var qq in q)
// I get FEBRUARY items here!!!
为什么我会在2月收到她的短信?我知道,可以用不同的方法完成,但是我很困惑为什么以这种方式编码时它会失败.注意:此处涉及的类型均为日期时间.
谢谢
Julio
Why do I get February itmes her?? I Know, it could be done differently, but I''m puzzled why it fails when coded this way. Note: types involved here are all datetime.
Thanks
Julio
推荐答案
您期望得到的结果是什么?
DateTime结构以以下格式显示值:
What is the result you are expecting?
The DateTime struct show values in this format:
DateTime(yyyy,mm,dd)
由于代码中的month为2,它将显示2月的值.
Since month is 2 in your code, it will show values from february.
您已使用01-Fab-2011初始化了DateTime
变量.
即使您减去1毫秒,它仍然保持01-Fab-2011.
因此,您将获得二月"项目.
确保要减去天/月或其他日期.
You have initialized yourDateTime
variable with 01-Fab-2011.
Even if you substract 1 Millisecond, it still remains 01-Fab-2011.
Hence you are getting "February" items.
Make sure if you want to substract Days/Months or something else.
浏览以下链接并获取C#DateTime基础.
DateTime.AddMilliseconds方法
DateTime.AddSeconds方法
终于
日期时间结构
System.DateTime方法(LINQ to SQL)
Navigate the following links and get the C# DateTime basics.
DateTime.AddMilliseconds Method
DateTime.AddSeconds Method
Finally
DateTime Structure
System.DateTime Methods (LINQ to SQL)
这篇关于日期时间比较错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文