· 8 years ago · May 14, 2018, 10:48 AM
1"create table if not exists data (pkey integer primary key, doc date)"
2
3[db executeUpdate:@"insert into data ( doc) values (?)" , [NSDate date]];
4
5"select * FROM data WHERE doc between '2009-08-23' and '2009-08-23' "
6
7sqlite> CREATE TABLE t (d DATETIME);
8sqlite> SELECT DATETIME('now');
92009-08-24 02:32:20
10sqlite> INSERT INTO t VALUES (datetime('now'));
11sqlite> SELECT * FROM t;
122009-08-24 02:33:42
13sqlite> SELECT * FROM t where d BETWEEN '2009-08-24' and '2009-08-24';
14sqlite> SELECT * FROM t where d BETWEEN '2009-08-24' and '2009-08-25';
152009-08-24 02:33:42
16
17NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
18[dateFormatter setDateFormat:@"yyyy-MM-dd"];
19NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
20NSDate *startOfDay = [dateFormatter dateFromString:dateString];
21// add a full day and subtract 1 second to get the end of day timestamp
22NSDate *endOfDay = [startOfDay addTimeInterval:(60*60*24)-1];
23[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSS"];