【发布时间】:2010-12-23 20:21:14
【问题描述】:
我在我的 iphone 应用程序中使用核心数据,但在某些情况下,我使用 sqlite 来访问数据并且我遇到了 NSDate 的问题。
NSDate *now = [NSDate date];
NSCalendar *calender = [NSCalendar currentCalendar];;
NSDateComponents *comp = [calender components:NSYearCalendarUnit fromDate:now];
[comp setDay:1];
[comp setMonth:1];
NSDate *yearAgo = [calender dateFromComponents:comp];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date > %@",yearAgo];
此代码有效并从年初开始选择记录,但如果我使用 sqlite 原始查询
NSDate *current=[NSDate date];
NSDateComponents *comps = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit |NSYearCalendarUnit) fromDate:current];
[comps setDay:1];
[comps setMonth:1];
NSDate *yearDate = [gregorian dateFromComponents:comps];
[where appendFormat:@"zdate > '%@' ",yearDate];
我有一个问题,由记录确定的错误日期,在 sqlite 中核心日期的日期为 2008-01-01 的记录的日期为 1977 年。 如何解决?在查询中使用 NSDate 可能是我错了吗?
【问题讨论】: