8

How do I filter events created for the current date in the Realm swift? I tried something like below but this wrong.

let dtSource = datasource.filter("Create == NSDate()").count

Update: Getting the filter creating my date as a string.

https://i.stack.imgur.com/8fLX9.png

https://i.stack.imgur.com/HDR2X.png

Fogh
  • 1,275
  • 1
  • 21
  • 29
cwilliamsz
  • 718
  • 9
  • 20
  • Check out this [question](http://stackoverflow.com/questions/35934243/filter-by-day-from-nsdate-in-realm-swift) and answers. – bcamur Mar 14 '16 at 07:31

1 Answers1

23

A query in the form of Create == NSDate() will check exact date equality, which will compare down to the second. If you want to check if a date is between a given interval, like checking if it's on a specific day, regardless of the time of day, you could do a BETWEEN check:

let dtSource = datasource.filter("Create BETWEEN %@", [firstDate, secondDate]).count

Update:

Here's a full code sample to get all date models for the current day:

import RealmSwift

class Event: Object {
    dynamic var date = NSDate()
}

let todayStart = Calendar.current.startOfDay(for: Date())
let todayEnd: Date = {
  let components = DateComponents(day: 1, second: -1)
  return Calendar.current.date(byAdding: components, to: todayStart)!
}()
events = realm.objects(Event.self).filter("date BETWEEN %@", [todayStart, todayEnd])
duan
  • 8,515
  • 3
  • 48
  • 70
jpsim
  • 14,329
  • 6
  • 51
  • 68
  • Hello, I got the way down but for that I need to save the date with string on the bench. let dtSource = datasource.filter("Create CONTAINS %@", valorBuscado).count – cwilliamsz Mar 13 '16 at 16:24
  • Is there any way to do the same search in a column of data type or convert the record date for string at the time of the search? let dtSource = datasource.filter(String(Create), "CONTAINS %@", valorBuscado).count – cwilliamsz Mar 13 '16 at 16:24
  • In this way I could not make it work yet: let ondeBuscar = "Criacao" let stringBusca = ondeBuscar + " = CONTAINS %@" let predicate = NSPredicate(format: stringBusca, "2016-03-13") let resultadoBusca = datasource.filter(predicate).count – cwilliamsz Mar 13 '16 at 16:29
  • You're trying to compare strings for dates in your comments here, which is the wrong way to go about doing this. I've added another code sample as an example for getting date models that fall on a given day. I suggest you read up on a bit more documentation for NSPredicates and NSDates. Here are some links: https://realm.io/news/nspredicate-cheatsheet/ , https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/AdditionalChapters/Introduction.html , https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/ – jpsim Mar 13 '16 at 23:27
  • Thank you for your help :) Getting the filter creating my date as a string. I will continue studying on the subject to improve. – cwilliamsz Mar 14 '16 at 15:47
  • does that BETWEEN needs `{} ` ? that is something like {, }. – Johnykutty Jan 17 '17 at 05:54
  • The curly braces are only needed when writing the predicate as a string, which I discourage. Instead, you should use the `%@` string interpolation as this answer shows. – jpsim Jan 17 '17 at 18:11