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

Change the format of a NSDate object

Hi there,

I'm trying to build a french version of the "Stormy" app. But I have a problem.

I want to change the format of the date which is, by default an american one, in a french one.

Here is what I've tried to do, but it doesn't work :/

Thank you !

import Cocoa
import Foundation



let unixTime : Int = 1418233178

let timeEnSeconde = NSTimeInterval(unixTime)
let dateMeteo = NSDate(timeIntervalSince1970: timeEnSeconde)

var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .MediumStyle

let template = "fr_FR"
dateFormatter.setLocalizedDateFormatFromTemplate(template)

println(dateFormatter.stringFromDate(dateMeteo))

1 Answer

Hello Nawfal,

Forecast API docs state that you should use the IANA timezone name for the requested location.

Note: the IANI website is not as user friendly as one would expect and requires you to download a zip file in order to determine what code to use. From what I have read you should be using (CET) Central European Time.

I used the following code to change the format of the time in my weather app Project RainMan

//Date formatter

func dateStringFromUnixtime(unixTime: Int) -> String {

    let timeInSeconds = NSTimeInterval(unixTime)
    let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)

    let dateFormatter = NSDateFormatter()
    dateFormatter.timeStyle = .MediumStyle

    return dateFormatter.stringFromDate(weatherDate)


}

I hope this helps :)

  • Happy Coding