5

I am trying to search records between two dates in swift. I'm storing the date as shown below:

     let date = NSDate()
     let dateFormatter = NSDateFormatter()
     dateFormatter.dateFormat = "yyyy'-'MM'-'dd HH':'mm':'ss"

     println(date) // prints 2015-09-26 05:22:41 +0000

     myreport.setValue(date, forKey: "date")

And below is my code to fetch the records between two dates using NSPredicate:

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"

    let startDate:NSDate = dateFormatter.dateFromString(fromdate)!
    let endDate:NSDate = dateFormatter.dateFromString(todate)!

    println(fromdate) // prints 2015-09-22
    println(todate)  // prints 2015-09-29
    println(startDate) // prints 2015-09-21 20:00:00 +0000
    println(endDate)  // prints 2015-09-28 20:00:00 +0000
    let predicate1 = NSPredicate(format: "%@ >= date AND %@ <= date", argumentArray: [startDate, endDate])

    let predicate = NSCompoundPredicate(type: NSCompoundPredicateType.OrPredicateType, subpredicates: [predicate1])
    // Set the predicate on the fetch request
    request.predicate = predicate
    request.sortDescriptors = [NSSortDescriptor(key: "report_id", ascending: false)]
    var results : NSArray = context.executeFetchRequest(request, error: nil)!

Note:

1) My fromdate and todate is in this format 2015-09-25 (as String)

2) My data type in core data for date attribute is NSDate

3) I am able to store and fetch the date from core data successfully

When I run above code Im not getting any error but the code is not searching for records between two dates. I mean im getting the empty results array.

Can any one plz tell me what im doing wrong here. Also plz let me know if any more information needed. Thanks in advance!

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
user3606682
  • 1,945
  • 3
  • 17
  • 24

2 Answers2

13

Finally after many modifications and testing this worked for me as shown below. If you need how im storing the nsdate in coredata plz check the question above.

    println(self.appdel.fromdate) // prints 2015-09-25
    println(self.appdel.todate) // prints 2015-09-26

    var fromdate = "\(self.appdel.fromdate) 00:00" // add hours and mins to fromdate 
    var todate = "\(self.appdel.todate) 23:59" // add hours and mins to todate

    var context : NSManagedObjectContext = appdel.managedObjectContext!
    var request = NSFetchRequest(entityName: "TblReportsP")
    request.returnsObjectsAsFaults = false
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
    dateFormatter.timeZone = NSTimeZone(name: "GMT") // this line resolved me the issue of getting one day less than the selected date
    let startDate:NSDate = dateFormatter.dateFromString(fromdate)!
    let endDate:NSDate = dateFormatter.dateFromString(todate)!
    request.predicate = NSPredicate(format: "(date >= %@) AND (date <= %@)", startDate, endDate)

    request.sortDescriptors = [NSSortDescriptor(key: "report_id", ascending: false)]
    var results : NSArray = context.executeFetchRequest(request, error: nil)!

    println(results.count)
user3606682
  • 1,945
  • 3
  • 17
  • 24
0

I think your predicate might be wrong, try modifying it like this: %@ <= date AND date <= %@

In this way you will look of date that is between start and end dates.

Lachezar
  • 6,523
  • 3
  • 33
  • 34
  • I tried ur solution above it still doesnt works. Also i have edited the question to add some print statements that how the date is printing before i save and how im using the dates while searching. One more thing is the date is getting decreased for one day when it is formatted. You can observe this in print statements above. – user3606682 Sep 26 '15 at 05:31