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 trialMatthew Turk
6,155 PointsKnotted Up In Formatting Time
On stage 3, challenge task 2, I was asked to properly format the date and I am confused with the .MediumStyle & .ShortStyle etcetera. Please help? Code is attached.
import Foundation
let secondsSinceBirth = NSTimeInterval(1200000000)
var dateOfBirth = NSDate(timeIntervalSince1970: secondsSinceBirth)
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var stringDate = "\(dateOfBirth)"
1 Answer
Vikram Chowdary
1,386 PointsTimeStyle and DateStyle are just the formatters which helps you format the NSdate value.
From the above code you get a NSdate value as follows : 2008-01-10 21:20:00 +0000
If you format the date using the TimeStyle and DateStyle that will return you an output as follows : Jan 10, 2008, 9:20 PM
How to achieve this :
**let secondsSinceBirth = NSTimeInterval(1200000000)
var dateOfBirth = NSDate(timeIntervalSince1970: secondsSinceBirth)
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var stringDate = dateFormatter.stringFromDate(dateOfBirth)***
Matthew Turk
6,155 PointsMatthew Turk
6,155 PointsThank you.