Guest User

Untitled

a guest
Dec 13th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. extension Date {
  2. var age: Int {
  3. return Calendar.current.dateComponents([.year], from: self, to: Date()).year!
  4. }
  5. }
  6.  
  7. extension DateFormatter {
  8. static func getDateFormatter() -> DateFormatter {
  9. let df = DateFormatter()
  10. df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
  11.  
  12. return df
  13. }
  14. }
  15.  
  16. let dob = "1993-10-14T00:00:00.000Z"
  17.  
  18. let date = DateFormatter.getDateFormatter().date(from: dob)
  19.  
  20. let calendar = Calendar.current
  21. let components = calendar.dateComponents([.year, .month, .day], from: date!)
  22.  
  23. let myDOB = calendar.date(from: DateComponents(year: components.year, month: components.month, day: components.day))!
  24.  
  25. let myAge = myDOB.age // 25
Add Comment
Please, Sign In to add comment