How can I filter a Realm List by Date Swift NSPredicates

Sergen Becerik

I want to filter my Realm List by Date. The Filter should show me every listed Item by month. I select the month "June" and the app should show me everything that was sold in June.

this is the code I tried but it obviously doesnt worked out.

let date = Date()
        let format = DateFormatter()
        format.dateFormat = "yyyy-MM-dd"
        let formattedDate = format.string(from: date)
        print(formattedDate)


        let current = Calendar.current
        let componentDate = current.component(.month, from: date)
        print(current.component(.month, from: date))

        let articles = realm.objects(Article.self).filter("artSoldDate = \(componentDate)")
        return articles
Dan Favano

You would need to use a date range filter. You would need to start the range at a specific date and end it at a certain date. For example,

let predicate = NSPredicate(format: "artSoldDate >= %@ AND artSoldDate <= %@", startDate, endDate)

let result = realm.objects(Art.self).filter(predicate)

Your example seems to try and check when the artSoldDate is exactly some date.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related