Calcola l’età dalla data di nascita utilizzando NSDateComponents in Swift

Sto cercando di calcolare l’età da data di nascita in Swift con questa funzione:

var calendar : NSCalendar = NSCalendar.currentCalendar() var dateComponentNow : NSDateComponents = calendar.components( NSCalendarUnit.CalendarUnitYear, fromDate: birthday, toDate: age, options: 0) 

Ma ottengo un errore Extra argument toDate in call

Nell’objective c questo era il codice, ma non so perché ottengo questo errore:

 NSDate* birthday = ...; NSDate* now = [NSDate date]; NSDateComponents* ageComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:birthday toDate:now options:0]; NSInteger age = [ageComponents year]; 

C’è una forma corretta migliore di questa?

Viene visualizzato un messaggio di errore perché 0 non è un valore valido per NSCalendarOptions . Per “nessuna opzione”, usa NSCalendarOptions(0) o semplicemente nil :

 let ageComponents = calendar.components(.CalendarUnitYear, fromDate: birthday, toDate: now, options: nil) let age = ageComponents.year 

(Specificare nil è ansible perché NSCalendarOptions conforms al protocollo RawOptionSetType che a sua volta eredita da NilLiteralConvertible .)

Aggiornamento per Swift 2:

 let ageComponents = calendar.components(.Year, fromDate: birthday, toDate: now, options: []) 

Aggiornamento per Swift 3:

Supponendo che i tipi Swift 3 siano usati per Date e Calendar :

 let now = Date() let birthday: Date = ... let calendar = Calendar.current let ageComponents = calendar.dateComponents([.year], from: birthday, to: now) let age = ageComponents.year! 

Creo questo metodo è molto semplice, basta inserire la data di compleanno nel metodo e questo restituirà l’età come Int

Swift 3

 func calcAge(birthday: String) -> Int { let dateFormater = DateFormatter() dateFormater.dateFormat = "MM/dd/yyyy" let birthdayDate = dateFormater.date(from: birthday) let calendar: NSCalendar! = NSCalendar(calendarIdentifier: .gregorian) let now = Date() let calcAge = calendar.components(.year, from: birthdayDate!, to: now, options: []) let age = calcAge.year return age! } 

Swift 2

 func calcAge(birthday: String) -> Int{ let dateFormater = NSDateFormatter() dateFormater.dateFormat = "MM/dd/yyyy" let birthdayDate = dateFormater.dateFromString(birthday) let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let now: NSDate! = NSDate() let calcAge = calendar.components(.Year, fromDate: birthdayDate!, toDate: now, options: []) let age = calcAge.year return age } 

uso

 print(calcAge("06/29/1988")) 

Questo funziona in swift 3 per me ..

 let now = NSDate() let calendar : NSCalendar = NSCalendar.current as NSCalendar let ageComponents = calendar.components(.year, from: datePickerView.date, to: now as Date, options: []) let age = ageComponents.year! ageCalculated.text = String(age) 

Grazie a @ Martin R

Questo funziona per Swift 3

 let myDOB = Calendar.current.date(from: DateComponents(year: 1994, month: 9, day: 10))! let myAge = Calendar.current.dateComponents([.month], from: myDOB, to: Date()).month! let years = myAge / 12 let months = myAge % 12 print("Age : \(years).\(months)")