update: Xcode 8.2.1 • Swift 3.0.2
extension Date { var age: Int { return Calendar.current.dateComponents([.year], from: self, to: Date()).year! } } let myDOB = Calendar.current.date(from: DateComponents(year: 1970, month: 9, day: 10))! let myAge = myDOB.age
Xcode 7.2 • Swift 2.1.1
You can use the components of the NSCalendar method to calculate the number of years from a specific date to today as follows:
extension NSDate { var age: Int { return NSCalendar.currentCalendar().components(.Year, fromDate: self, toDate: NSDate(), options: []).year } } let myDOB = NSCalendar.currentCalendar().dateWithEra(1, year: 1970, month: 09, day: 10, hour: 0, minute: 0, second: 0, nanosecond: 0)! let myAge = myDOB.age
source share