Calculate Age (Returns Only Years)

PHOTO EMBED

Sat Feb 19 2022 04:44:50 GMT+0000 (Coordinated Universal Time)

Saved by @hasnat #ios #swift #calculate #age

let age = calcAge(birthday: "789089362000") // pass timeStamp in mili seconds
print("Age = \(age)")

func calcAge(birthday: String) -> Int {
    let seconds = (Int(birthday)!/1000) as NSNumber?
    let timeStampDate = Date(timeIntervalSince1970: seconds!.doubleValue)
    let calendar: NSCalendar! = NSCalendar(calendarIdentifier: .gregorian)
    let now = Date()
    let calcAge = calendar.components(.year, from: timeStampDate, to: now, options: [])
    let age = calcAge.year
    return age!
  }
content_copyCOPY