Skip to content

Instantly share code, notes, and snippets.

@ispiropoulos
Last active September 29, 2016 10:17
Show Gist options
  • Save ispiropoulos/3307549694daea3d3573797bec91b523 to your computer and use it in GitHub Desktop.
Save ispiropoulos/3307549694daea3d3573797bec91b523 to your computer and use it in GitHub Desktop.
Get age from (Birth) Date object
//
// Date+Age.swift
//
//
// Created by John Spiropoulos on 29/09/16.
// Copyright © 2016 John Spiropoulos. All rights reserved.
//
/*
Swift 3 Helper extension:
usage example:
let myBirthday = Calendar.current.date(from: DateComponents(year: 1985, month: 7, day: 15))!
let myAge = myBirthday.age // 31
*/
extension Date {
var age: Int {
return Calendar.current.dateComponents([.year], from: self, to: Date()).year!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment