Im new at swift programming and i havent been able successfully find code to find difference between two dates in terms of years , months and days.
I tried the following code but it didnt work
let form = NSDateComponentsFormatter()
form.maximumUnitCount = 2
form.unitsStyle = .Full
let s = form.stringFromTimeInterval( date2.timeIntervalSinceReferenceDate - date1.timeIntervalSinceReferenceDate)
If you need the difference as a (localized) string to present it to the user,
NSDateFormatter like this
let form = NSDateComponentsFormatter() form.maximumUnitCount = 2 form.unitsStyle = .Full form.allowedUnits = [.Year, .Month, .Day] let s = form.stringFromDate(date1, toDate: date2)
As already mentioned in the comments, computing the difference from the pure time interval between the dates cannot give correct results because most information about the dates is lost.