1

I am trying to retrieve records which were added today to parse, but the query does not return any results, I tried with below code,

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

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

can anyone help me please?

Parth Pandya
  • 1,460
  • 3
  • 18
  • 34
loulou milo
  • 107
  • 1
  • 2
  • 6

1 Answers1

1

With equalTo, you're just querying objects created at exactly 12:00am. You should query the range of dates from the 12:00am of today till 12:00am of tomorrow.

Use Parse's lessThanOrEqualTo and greaterThanOrEqualTo.

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)
kRiZ
  • 2,320
  • 4
  • 28
  • 39