Last active
November 23, 2018 07:53
-
-
Save muhlenXi/fd74fee2d418971ddb42a67397b95c02 to your computer and use it in GitHub Desktop.
通过 Calendar 计算今年几岁?
This file contains 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
/// 计算今年年龄 eg: 1992-03-19 | |
func calculateThisYearAge(birthday: String) -> Int?{ | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd" | |
if let birthday = dateFormatter.date(from: birthday) { | |
let gregorian = Calendar(identifier: .gregorian) | |
let components = gregorian.dateComponents([Calendar.Component.year], from: birthday, to: Date()) | |
if let year = components.year { | |
return year + 1 | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment