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

Matthew Turk
Matthew Turk
6,155 Points

Knotted 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.

date_formatter.swift
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
Vikram Chowdary
1,386 Points

TimeStyle 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)***