Asp.net 比较查询中的两种不同日期格式
我必须将用户输入的日期“Dt”(mm/dd/yyyy格式)与RavenDB中的日期“ReleaseDate”(时间戳,如“/date(1187668800000)/”)进行比较。为此,我使用下面的代码,几乎完成了工作,但我不需要什么帮助来完成松散的结尾 如何比较这两个日期,以便成功运行查询Asp.net 比较查询中的两种不同日期格式,asp.net,datetime,timestamp,ravendb,Asp.net,Datetime,Timestamp,Ravendb,我必须将用户输入的日期“Dt”(mm/dd/yyyy格式)与RavenDB中的日期“ReleaseDate”(时间戳,如“/date(1187668800000)/”)进行比较。为此,我使用下面的代码,几乎完成了工作,但我不需要什么帮助来完成松散的结尾 如何比较这两个日期,以便成功运行查询 public ActionResult Calculation(DateTime? Dt) { var store = new DocumentSto
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);
}
公共行动结果计算(日期时间?Dt)
{
var store=newdocumentstore{Url=”http://localhost:80" };
store.Initialize();
var CalcModel=新的CalcViewModel();
使用(var session=store.OpenSession())
{
//将用户以mm/dd/yyyy格式输入的日期dt转换为总计
//毫秒-以便稍后我可以将此值与RavenDB进行比较
//时间戳日期格式(旧版本)
DateTime d1=新的日期时间(1970,1,1);
DateTime d2=Dt.Value.ToUniversalTime();
TimeSpan ts=新的TimeSpan(d2.Ticks-d1.Ticks);
double tmillisecs=ts.TotalMillisecs;//尚未使用此值。
CalcModel.MoviesByDate=session.Query()
.Where(x=>x.ReleaseDate.Ticks==ts.Ticks)
.Count();
//这是我需要比较两个日期的地方-ts.ticks给出了
//日期要求值(1187668800000)乘以10000。
}
返回视图(CalcModel);
}
现在,当我调试时,我知道ts.ticks显示的是什么值。。。就像我在上面的代码注释中说的,所需的值乘以10000。但我在运行时不知道x.ReleaseDate或x.ReleaseDate.Ticks中的值是多少。。我做得对吗。谢谢你的帮助。嗯。。。我认为您严重误解了SQL日期是如何工作的,以及它是如何应用于.NET的。关于日期的全部要点是,它们是以数字格式存储的,而不是文本格式。因此,当您有一个DateTime对象时,它不是存储为文本日期,而是存储为数字类型,您可以将其转换为您想要的任何格式 由于.net提供程序将数据库本机datetime对象转换为datetime对象,因此您只需对它们进行本机比较即可。即:
DateTime d1 = new DateTime(1970, 1, 1);
CalcModel.MoviesByDate = session.Query<Movies>()
.Where(x => x.ReleaseDates.Date == d1.Date)
.Count();
DateTime d1=新日期时间(1970,1,1);
CalcModel.MoviesByDate=session.Query()
.其中(x=>x.ReleaseDates.Date==d1.Date)
.Count();
无论RavenDB如何在内部存储日期,当DateTime对象在查询中具体化时,它将采用本机.NET格式。您的意思是。其中(x=>x.ReleaseDates.Date==d2.Date)而不是d1.Date?正确的?我在这方面运气不太好。。让我再试一次。谢谢。@ZVenue-日期就是日期,不管什么时间。。除非您也在比较时间,在这种情况下,请关闭.Date部分并使用通用时间(假设您在通用时间中输入了日期时间)是的。。这起作用了。非常感谢。是DateTime d2=Dt.Value.ToUniversalTime()的代码行;做这件事很重要。。或者我可以直接在查询中使用Dt(参数值)吗?比如.Where(x=>x.ReleaseDates.Date==dt.Date)@ZVenue-No,您不需要这行。但如果只是比较日期,则应将其更改为
Dt.Value.Date
。如果Dt为Null,则还可能存在Null引用异常,因此需要对此进行一些错误检查,或者如果为Null,则提供一个默认值。或者,如果该值不能为null,则将该方法更改为使用普通DateTime.Great。非常感谢。我已经准备好了空检查。这很有效。