Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Stephen Becker
7,056 PointsFrom Date - Birthday = Age
Hey Guys,
I have been trying to figure out with Swift, how I can make a function that makes a NSDate() - Birthday = Age
Here is a example of my current code
//: Account Details
import UIKit
let date = NSDate()
struct AccountDetails {
let firstName: String
let lastName: String
let birthday: String
func fullName () -> String {
return firstName + " " + lastName
func age () - > Int {
return date - birthday
}
}
let johnSmith = AccountDetails(firstName: "John", lastName: "Smith", birthday: "February 3rd, 1989")
1 Answer
Hayden Pennington
6,154 PointsThere are several ways to approach a solution. I have two options, and hopefully you can use one!
Depending on how much supporting code you have, you could change the struct AccountDetails, to store the birthday as Int values. Then use my this solution:
// Create an instance of NSCalender with currentCalender
let calender = NSCalendar.currentCalendar()
// Set the components to the birthday
let dateComponents = NSDateComponents()
dateComponents.day = 21
dateComponents.month = 8
dateComponents.year = 1980
// Create an NSDate object from the birthday
let birthday = calender.dateFromComponents(dateComponents)
// Get today's date
let today = NSDate()
// Use the NSCalender method to get the difference (e.g today - birthday)
let age = calender.components(NSCalendarUnit.CalendarUnitYear,
fromDate: birthday!,
toDate: today,
options: nil).year // Be sure to put .year
If you would prefer to store the birthday as a String, you could create a parsing method that takes a String as a parameter and returns a tuple with the the Int values.
Or store a variant of the String and use the following solution:
// This would be the var in AccountDetails
let birthdayString = "1980-08-21"
// Create a Formatter
let dateFormatter = NSDateFormatter()
// Set the dateFormat. Browse the documentation for variant formats
dateFormatter.dateFormat = "yyyy'-'MM'-'dd"
// Create an NSDate holding the birthday, with the dateFormatter.
let birthday = dateFormatter.dateFromString(birthdayString)
// Get today's date
let today = NSDate()
// Create an instance of NSCalender with currentCalender
let calender = NSCalendar.currentCalendar()
// Use the NSCalender method to get the difference (e.g today - birthday)
let age = calender.components(NSCalendarUnit.CalendarUnitYear,
fromDate: birthday!,
toDate: today,
options: nil).year // Be sure to put .year
It seems like there isn't a clean way of doing this (Coming from an inexperienced IOS developer).
Hope this helps!