Swift: Parse, query date field based on nsdate ()
I am trying to get records that were added today for parsing, but the query is returning no results. how can I get a query to return results based on today's date.
let now = NSDate()
var query = PFQuery(className:"userBids")
query.whereKey("date", equalTo: now)
the parse date field is set to date. please, help
+3
source to share
1 answer
Your problem is that NSDate
it's not just a date, it's an exact point in time.
And you most likely will not have any records from this exact date and time.
What you should be doing is something like:
let now = NSDate()
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let midnightOfToday = cal.startOfDayForDate(now)
var query = PFQuery(className:"userBids")
query.whereKey("date", greaterThanOrEqualTo: midnightOfToday)
The above solution only works for iOS 8 and newer ( and I found it here ). Click this link if you want something that is compatible with iOS 7.
+3
source to share