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

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