我必须将用户输入的日期“Dt”(以mm/dd/yyyy格式)与RavenDB中的日期进行比较 - “ReleaseDate”(时间戳记如“/ Date(1187668800000 )/“)。为此,我使用下面的代码几乎完成了工作,但我需要一点帮助来确定松散的末端...在查询中比较两种不同的日期格式
如何比较两个日期,以便我可以使查询成功运行。
public ActionResult Calculation(DateTime? Dt)
{
var store = new DocumentStore { Url = "http://localhost:80" };
store.Initialize();
var CalcModel = new CalcViewModel();
using (var session = store.OpenSession())
{
//Converting user entered date dt in mm/dd/yyyy format to total
//milliseconds - So that later I can compare this value to RavenDB
//time stamp date format (older versions)
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = Dt.Value.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
double tmillisecs = ts.TotalMilliseconds; //Not yet using this value.
CalcModel.MoviesByDate = session.Query<Movies>()
.Where(x => x.ReleaseDate.Ticks == ts.Ticks)
.Count();
// this is where I need to compare two dates - ts.ticks gives the
// required value of date (1187668800000) multiplied by 10000.
}
return View(CalcModel);
}
现在,当我调试,我知道什么样的价值ts.ticks正显示出...和我一样在代码中的注释上面说的,所需要的值乘以10000。但我在运行任何线索时间,x.ReleaseDate中的值是或x.ReleaseDate.Ticks是..我正确地做到了这一点。谢谢您的帮助。
您的意思.Where(x => x.ReleaseDates.Date == d2.Date)而不是d1.Date?对?我没有太多的运气,让我再试一次。谢谢。 – ZVenue 2011-12-28 20:33:40
@Zenue - 日期是一个日期,时间是什么都没有关系。除非你比较时间,在这种情况下,关闭.Date部分并使用世界时(假设你将日期时间输入为通用时间) – 2011-12-28 20:37:06
是的..这工作。谢谢。是代码行DateTime d2 = Dt.Value.ToUniversalTime();重要的是要做到这一点..或者我可以直接在查询中使用Dt(参数值)?像.Where(x => x.ReleaseDates.Date == dt.Date) – ZVenue 2011-12-28 20:40:11