Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extension Date {
- var age: Int {
- return Calendar.current.dateComponents([.year], from: self, to: Date()).year!
- }
- }
- extension DateFormatter {
- static func getDateFormatter() -> DateFormatter {
- let df = DateFormatter()
- df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
- return df
- }
- }
- let dob = "1993-10-14T00:00:00.000Z"
- let date = DateFormatter.getDateFormatter().date(from: dob)
- let calendar = Calendar.current
- let components = calendar.dateComponents([.year, .month, .day], from: date!)
- let myDOB = calendar.date(from: DateComponents(year: components.year, month: components.month, day: components.day))!
- let myAge = myDOB.age // 25
Add Comment
Please, Sign In to add comment