Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Weather App with Swift (Retired) Data Modeling With Structures Customizing Dates

At a loss currently with this piece of code. Would be great if anyone could help!

Not quite sure what I have done wrongly.

date_formatter.swift
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"

1 Answer

Hi Jamal,

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 dateStyle and 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!

Steve.

Thanks Steve.

Really appreciate the help!

Jamal