Sqlite-net-PCL比较日期

发布于 2025-01-23 21:57:44 字数 1416 浏览 1 评论 0原文

如问题所述,我试图对在特定日期范围内完成的任务进行日期比较。

我的模型有一个名为“完成”的布尔值和一个存储完整日期的字符串,称为“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷遇一生 2025-01-30 21:57:44

假设在[datesCompleted]您将时间戳以ISO格式存储yyyy-mm-dd hh:mm:mm:ss,正确的sqlite语法,以便在7天之前7天获取时间戳当前的时间戳为:

datetime('now', '-7 day')

或:

datetime('now', '-7 day', 'localtime')

要在当地时刻获取时间戳。

因此您的查询应该是:

SELECT * 
FROM [Tasks] 
WHERE [Done] = 1 AND [DatesCompleted] >= datetime('now', '-7 day')

Assuming that in [DatesCompleted] you store timestamps in ISO format YYYY-MM-DD hh:mm:ss, the correct SQLite syntax to get a timestamp exactly 7 days before the current timestamp is:

datetime('now', '-7 day')

or:

datetime('now', '-7 day', 'localtime')

to get the timestamp in local time.

so your query should be:

SELECT * 
FROM [Tasks] 
WHERE [Done] = 1 AND [DatesCompleted] >= datetime('now', '-7 day')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文