2016-01-03 56 views
1

我試圖找回這是今天加入解析記錄提取使用NSDate的數據,但查詢不返回任何結果,我試着用下面的代碼,如何從解析

let date = NSDate() 
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
let Today = calendar!.startOfDayForDate(date) 

query.whereKey("createdAt", equalTo: Today) 

誰能幫助我請?

+0

您創建的密鑰恰好在上午12:00?否則我不認爲它不會匹配。 – mn1

+0

不!我該如何解決它? –

+0

試試這個http://stackoverflow.com/questions/31379063/swift-parse-query-date-field-based-on-nsdate – mn1

回答

1

隨着equalTo,你只是查詢在上午12:00創建的對象。 您應該查詢從今天中午12:00到明天中午12:00的日期範圍。

使用分析的lessThanOrEqualTogreaterThanOrEqualTo

let date = NSDate() 
let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 

let today = calendar!.startOfDayForDate(date) 

// calculate tomorrow 
date = NSCalendar.currentCalendar().dateByAddingUnit(
        .CalendarUnitDay, 
        value: 1, 
        toDate: date, 
        options: NSCalendarOptions(0)) 

let tomorrow = calendar!.startOfDayForDate(date) 

query.whereKey("createdAt", greaterThanOrEqualTo:today) 
query.whereKey("createdAt", lessThanOrEqualTo:tomorrow)