iOS Life

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Calculate Age in Swift

For a new application I’m working on I needed to capture the birth date of a user and validate if they are old enough to use the app.

Below is a simple method in Swift to return the age of a user after passing in the users birthday.

func calculateAge (birthday: NSDate) -> NSInteger {
    
    var userAge : NSInteger = 0
    var calendar : NSCalendar = NSCalendar.currentCalendar()
    var unitFlags : NSCalendarUnit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay
    var dateComponentNow : NSDateComponents = calendar.components(unitFlags, fromDate: NSDate.date())
    var dateComponentBirth : NSDateComponents = calendar.components(unitFlags, fromDate: birthday)
    
    if ( (dateComponentNow.month < dateComponentBirth.month) ||
        ((dateComponentNow.month == dateComponentBirth.month) && (dateComponentNow.day < dateComponentBirth.day))
        )
    {
        return dateComponentNow.year - dateComponentBirth.year - 1
    }
    else {
        return dateComponentNow.year - dateComponentBirth.year
    }
}

To call the method and print out the return value in the console.

println("User Age: \(calculateAge(self.datePicker.date))")
Jul 10, 2014 Brian Coleman
Tutorial: Post to Web Server API in Swift using NSURLConnectionPlay LED Baseball
You Might Also Like
 
Tutorial: Building an Apple Watch Glance
 
Tutorial: Building an Apple Watch Notification
6 years ago Swift, Tutorialsios8, swift, tutorial7,628
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
brianjcoleman on Twitter
  • Classix is still holding in the top charts on Apple TV in Canada. #55 Free, #23 Top Grossing. #tvos #appletv #app https://t.co/xuEJiT4rro, Jul 14
  • New Blog Post: "Classix for iPhone, iPad & Apple TV” #iOSDev #ios #swift #swiftlang #SwiftDevs #AppleTV Read here: https://t.co/uF6w3gYOot, May 20
  • New Blog Post: "How to test your app for IPv6 compatibility” #iOSDev #ios #swift #swiftlang #SwiftDevs Read here: https://t.co/SveichSUep, May 6

Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideostrategygamesframeworknewsappsmonitizeios7applefacebookwatchtoolstvosios9bookdesignsocialapiprovisiontutorialsbooksiapiTunes ConnectIPv6
Search
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.
MOST VIEWED
Tutorial: How To Use Login in Facebook SDK 4.1.x for Swift
162,592 views
Tutorial: How to test your app for IPv6 compatibility
99,843 views
Tutorial: How to use Auto Layout in Xcode 6
88,709 views
FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2013-2017 © Brian Coleman