Sqlite-net-PCL比较日期
如问题所述,我试图对在特定日期范围内完成的任务进行日期比较。
我的模型有一个名为“完成”的布尔值和一个存储完整日期的字符串,称为“ ditatescompleted”
我目前有一个查询,这表明我所有完成的项目都限制在25中,它效果很好:
return db.QueryAsync<Tasks>("SELECT * FROM [Tasks] WHERE [Done] = 1 LIMIT 25");
我如何才能完成只能完成该日期。是今天的7天吗?这是一种sudo代码,我希望如何获得解决方案,但我不知道如何在sqlite-net-pcl中写下它,
return db.QueryAsync<Tasks>("SELECT * FROM [Tasks] WHERE [Done] = 1 AND [DatesCompleted] >= (DateTime.Now - new TimeSpan(7,0,0,0))` LIMIT 25");
编辑: 尝试第一个解决方案后,我没有运气,所有项目似乎都出现了。我尝试了这一点:
foreach(var ii in TaskListDoneSource)
{
System.Diagnostics.Debug.WriteLine(ii.Name + " completed on : " + ii.DateCompleted + " compared against " + (DateTime.Now-new TimeSpan(7,0,0,0)).ToString());
if (ii.DateCompleted > (DateTime.Now - new TimeSpan(7, 0, 0, 0)))
System.Diagnostics.Debug.WriteLine("The date completed has not quite hit 7 days");
}
if语句向我表明,当我在7天内我是在工作时起作用的,但显然没有if语句呼叫。所以告诉我,我在第一个答案中建议的查询我做错了什么:
从[tasks]中选择 *,其中[完成] = 1和[dateCompleted]&gt; = datetime('现在','-7天')
编辑2:我可以处理提供的查询。唯一关闭的是将数据填充作为日期时间,而不是在我的模型中正确格式化的字符串。 ( https://www.sqlite.org/lang_datefunc.html )
As stated in the question I am attempting to do a date comparison for tasks that are done within a specific date range.
My model has a boolean called "Done" and a string that stores dates of completion called "DatesCompleted"
I currently have a query on that shows me all Done items limited to 25 and it works just fine:
return db.QueryAsync<Tasks>("SELECT * FROM [Tasks] WHERE [Done] = 1 LIMIT 25");
How can I get only dates completed that are 7 days prior to today? This is kind of sudo code how I'd expect to get the solution but I don't know how to write it out in SQLite-net-pcl,
return db.QueryAsync<Tasks>("SELECT * FROM [Tasks] WHERE [Done] = 1 AND [DatesCompleted] >= (DateTime.Now - new TimeSpan(7,0,0,0))` LIMIT 25");
Edit:
After attempting the first solution I am having no luck, all items regardless of completion time appear to show up. I tried this:
foreach(var ii in TaskListDoneSource)
{
System.Diagnostics.Debug.WriteLine(ii.Name + " completed on : " + ii.DateCompleted + " compared against " + (DateTime.Now-new TimeSpan(7,0,0,0)).ToString());
if (ii.DateCompleted > (DateTime.Now - new TimeSpan(7, 0, 0, 0)))
System.Diagnostics.Debug.WriteLine("The date completed has not quite hit 7 days");
}
The IF statement shows me that it is working when I am within 7 days but outside obviously no IF statement call. So that tells me I'm doing something wrong with the query recommended in first answer:
SELECT * FROM [Tasks] WHERE [Done] = 1 AND [DateCompleted] >=
datetime('now', '-7 day')
EDIT 2: I got it working with that query provided. Only thing that was off was off was having the DateCompleted as a DateTime instead of a string that was formatted properly in my model. (https://www.sqlite.org/lang_datefunc.html)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设在
[datesCompleted]
您将时间戳以ISO格式存储yyyy-mm-dd hh:mm:mm:ss
,正确的sqlite语法,以便在7天之前7天获取时间戳当前的时间戳为:或:
要在当地时刻获取时间戳。
因此您的查询应该是:
Assuming that in
[DatesCompleted]
you store timestamps in ISO formatYYYY-MM-DD hh:mm:ss
, the correct SQLite syntax to get a timestamp exactly 7 days before the current timestamp is:or:
to get the timestamp in local time.
so your query should be: