I have a database table and storing a datetime column with the UTC datetime, iOS [NSDate date] is returning always a UTC datetime. It's all good about it.
I'm using FMDB to get the last 12 and 24 hours data with the following code.
[self.fmQueue inDatabase:^(FMDatabase *db)
NSString* queryCommand = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ >= Datetime('?')", readingsTable, dateTimeRecordedColumn];
NSDate* dateBefore24Hours = [self.dateUtilities dateBySubtractingDays:1 forDate:[NSDate date]];
WEELogDebug(@"Date before 24 hours: %@", dateBefore24Hours);
FMResultSet* result = [db executeQuery:queryCommand, dateBefore24Hours];
Date before 24 hours: 2015-04-06 15:28:08 +0000
2015-04-07 15:28:08 +0000
You do not need the
Datetime() function. (And even if you did, one would never put the
? within quotation marks.)
When FMDB encounters a
NSDate object in the list of parameters, it will automatically do
timeZoneForSecondsFromGMT (if you didn't specify a
dateFormat string), and so this numeric representation of the date can be used when comparing to your
dateTimeRecorded column. By the way, this numeric value can be accessed in SQLite as a
Bottom line, you should simply be able to use
SELECT * FROM %@ WHERE %@ >= ?