gpt4 book ai didi

ios - 查询日期约束

转载 作者:行者123 更新时间:2023-11-28 19:58:17 25 4
gpt4 key购买 nike

我已经对这个问题进行了研究,并认为我找到了有意义的答案,但它不起作用,所以我不确定我是否犯了一个愚蠢的错误。我想查询过去一周、一个月、一年等创建的数据。我查看了我已经上传的数据进行解析和测试,我想查询过去 5 天的数据。出于某种原因,它在 8 月 8 日之后应该创建五个对象时却找到了 0 个对象。当我切换到 lessThanOrEqualTo 时,它找到了所有 9 个对象,即使在 8 月 8 日之前创建了 4 个对象。关于这一点的任何澄清。谢谢

NSDate *then=[NSDate dateWithTimeIntervalSinceNow:-7200];
PFQuery *query=[PFQuery queryWithClassName:@"urinationAmount"];
[query whereKey:@"user" equalTo:[PFUser currentUser]];
[query whereKey:@"createdAt" greaterThanOrEqualTo:then];
urinationDataArray=[query findObjects];
NSLog(@"Objects in Array:%i",urinationDataArray.count);

最佳答案

根据 Date and Time Programming Guide ,您不应该手动进行日历计算。

引自同一文件说:

NSDate provides the absolute scale and epoch for dates and times, which can then be rendered into a particular calendar for calendrical calculations or user display. To perform calendar calculations, you typically need to get the component elements of a date, such as the year, the month, and the day. You should use the provided methods for dealing with calendrical calculations because they take into account corner cases like daylight savings time starting or ending and leap years.

所以五天前应该是这样的:

NSCalendar * currentCalendar = [NSCalendar currentCalendar];
NSDateComponents * dateComponents = [NSDateComponents new];
NSDate * now = [NSDate date];

[dateComponents setDay:-5];//five days ago

NSDate * fiveDaysAgo = [currentCalendar dateByAddingComponents:dateComponents toDate:now options:0];

可以查看NSDateComponents Class Reference了解可用于计算的更多日历单位。

关于ios - 查询日期约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25290542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com