2

I have an array of dates like this :-

  var dateArray = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]

and I want to find out the difference of days between them. I do some research and i am able to do that with only two dates here is the approach i did in finding the difference between two date

let dateFormatter = DateFormatter()
let isoDate = "2016-04-20"
let calendar = NSCalendar.current
let currentDate = Date()

And in my viewDidLoad method I did this

    override func viewDidLoad() {
    super.viewDidLoad()



//  let components = calendar.dateComponents([.day], from: )
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") //en_US_POSIX
    let formatedStartDate = dateFormatter.date(from: isoDate)
    let date = dateArray.compactMap { dateFormatter.date(from: $0) } // for date array
    print(date)
    let components = Set<Calendar.Component>([.day])
    let differenceOfDate = Calendar.current.dateComponents(components, from: formatedStartDate!, to: currentDate )

    print (differenceOfDate)
    apiData()
}

As you can see in the code i created a constant let isoDate = "2016-04-20" and changed into formatedStartDate and find the difference between form this date to current date then it worked. But what if I have my own array of dates and how can i find the difference of my own array of dates and sort it into increasing or decreasing order. Please help?

2 Answers 2

4
  • Create the date formatter

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.locale = Locale(identifier: "en_US_POSIX") //en_US_POSIX
    
  • Map the date string array to Date instances

    let dateStringArray = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]
    let dateArray = dateStringArray.map {dateFormatter.date(from: $0)!} // add .sorted() if the array is unordered.
    
  • In a loop get the differences between adjacent items

    var differences = [Int]()
    for i in 0..<dateArray.count - 1 {
        let dayComponent = Calendar.current.dateComponents([.day], from: dateArray[i], to: dateArray[i+1])
        differences.append(dayComponent.day!)
    }
    print(differences)
    
Sign up to request clarification or add additional context in comments.

2 Comments

It worked Sir but one thing i want to do more I want to print that dates in decreasing order. How can I do that?
print(Array(differences.reversed())) or append .sorted(by: >) to the dateStringArray line and swap [i] with [i+1]
1

I'm not sure if you only want to compare the dates next to each other or compare each and every date. In the first case go with vadians solution. In the other case you can do something like this:

let dateStrings = ["2016-04-20", "2016-04-22", "2016-04-25", "2016-04-30"]

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")

let dates = dateStrings.compactMap { dateFormatter.date(from: $0) }

let calendar = Calendar.current

var differences: [Int] = []
for i in 0..<dates.count {
    for j in i + 1..<dates.count {
        if let difference = calendar.dateComponents([.day], from: dates[i], to: dates[j]).day {
            differences.append(difference)
        }
    }
}

let descendingDifferences = differences.sorted(by: >)
print(descendingDifferences) // results in [10, 8, 5, 5, 3, 2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.