Last active
September 29, 2016 10:17
-
-
Save ispiropoulos/3307549694daea3d3573797bec91b523 to your computer and use it in GitHub Desktop.
Get age from (Birth) Date object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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