Nicolai Harbo
Nicolai Harbo

Reputation: 1169

How to get current day in another language than your locale?

I want to get the name of the current day in english, but my locale is Danish, so NSDate() is returning the current day in danish.. How can I get it in english? :-)

Right now im using this code to get the name of the day:

func dayOfTheWeek() -> String? {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.stringFromDate(self)
}

I've tried looking at the documentations for NSDate, but couldn't really find anything that worked.

I've also looked at Calendar, but still no luck. I guess it is possible - so if anyone knows how, please let me know :-)

Upvotes: 0

Views: 721

Answers (1)

rmaddy
rmaddy

Reputation: 318794

You just need to set the formatter's locale:

func dayOfTheWeek() -> String? {
    let dateFormatter = NSDateFormatter()
    dateFormatter.locale = NSLocale(localeIdentifier: "en_US") // or "en_GB"
    dateFormatter.dateFormat = "EEEE"
    return dateFormatter.stringFromDate(self)
}

You seem to be using Swift 2 and I based the NSLocale initializer on Swift 3. Adjust if needed.

Upvotes: 2

Related Questions