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!
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
At a loss currently with this piece of code. Would be great if anyone could help!
Not quite sure what I have done wrongly.
import Foundation let secondsSinceBirth = NSTimeInterval(1200000000) var dateOfBirth = NSDate(timeIntervalSince1970: secondsSinceBirth) let dateFormatter = NSDateFormatter() dateFormatter.timeStyle = .ShortStyle dateFormatter.dateStyle: NSDateFormatterMediumStyle return dateFormatter.stringFromDate(dateOfBirth) var stringDate = "Jan 2, 2015"
The code you need looks like:
import Foundation let secondsSinceBirth = NSTimeInterval(1200000000) var dateOfBirth = NSDate(timeIntervalSince1970: secondsSinceBirth) var dateFormatter = NSDateFormatter(); dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle dateFormatter.timeStyle = .ShortStyle let stringDate = dateFormatter.stringFromDate(dateOfBirth)
So, first we create an instance of the NSDateFormatter, as you have done.
Next, we access the
timeStyle and set the appropriate style, as required. Lastly, we use the
stringFromDate method and pass it the string that was provided.
I hope that all makes sense!