Snippets Collections
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!
  }
star

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

#ios #swift #calculate #age

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension