0

As I am new to swift, I stuck here from very longer,I am trying to get date from array between start date and end date,.

I have tried these code which refered from [https://stackoverflow.com/questions/10216991/fetching-dates-which-comes-between-startdate-and-enddate], But I am getting the error as "Value of type '[Date]' has no member 'filtered'".

In swift

var predicate = NSPredicate(format: "(SELF > %@) AND (SELF < %@)", startdate!, enddate!)
var result = arrayWithDates.filtered(using: predicate)

Expected output: 
dateArray = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
startdate = "13/01/1996" 
enddate = "17/01/1996"
output = ["13/01/1996","15/01/1996","17/01/1996"]

please help me to acheive this, thanks in advance

Mr.Javed Multani
  • 12,549
  • 4
  • 53
  • 52
Swift_prasad
  • 65
  • 1
  • 11

3 Answers3

0

You can do it this way:

import Foundation

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")

let dateArray = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
let startdate = dateFormatter.date(from: "13/01/1996")!
let enddate = dateFormatter.date(from: "17/01/1996")!

let output = dateArray
    // Map your strings to Date objects excluding nils.
    .compactMap { dateFormatter.date(from: $0) }
    // Filter the mapped array by your condition.
    .filter { $0 >= startdate && $0 <= enddate }
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
0

You can try below code, it works for me:

class ViewController: UIViewController {
var arrDates = NSArray()
var arrResultDates = NSMutableArray()
override func viewDidLoad() {
    super.viewDidLoad()

   arrDates = ["12/01/1996","13/01/1996","15/01/1996","17/01/1996"]
    let strStartDate = "\(arrDates.firstObject!)"
    let strEndDate = "\(arrDates.lastObject!)"
    let formatter  = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    //Get start date
    var startDate = formatter.date(from: strStartDate)
    let endDate = formatter.date(from: strEndDate) // last date

    // Formatter for printing the date, adjust it according to your needs:
    let fmt = DateFormatter()
    fmt.dateFormat = "dd/MM/yyyy"
    let calendar = NSCalendar.current
    while startDate! <= endDate! {

        startDate = calendar.date(byAdding: .day, value: 1, to: startDate!)!
        let strDate = fmt.string(from: startDate!)
        if arrDates.contains(strDate)
        {
            arrResultDates.add(strDate)
        }
    }

    print(arrResultDates)

}

Hope it will helps you :)

Anjali jariwala
  • 410
  • 5
  • 15
0

If you really need to use NSPredicate you have to bridge the array to NSArray which responds to filtered(using:

let predicate = NSPredicate(format: "(SELF > %@) AND (SELF < %@)", startdate!, enddate!)
let result = (arrayWithDates as NSArray).filtered(using: predicate)

However in the world of Swift this is not recommended. Just use the filter function with a closure

let result = arrayWithDates.filter { $0 >= startdate! && $0 <= enddate! }
vadian
  • 274,689
  • 30
  • 353
  • 361