4

I have a String that is "Jueves 14 Junio 1990": it is a date formatted in the es_MX locale, I want to convert that into a new string with en_US as the locale.

This is my code:

let myString:String = "Jueves 14 Junio 1990"

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
dateFormatter.dateFormat = "EEEE dd MMMM yyyy"
let finalDate:NSDate = dateFormatter.dateFromString(myString)!
let finalString = dateFormatter.stringFromDate(date)

I was waiting that finalString resulted as Thursday 14 June 1990.

The problem here is that finalDate is nil.

Is my code wrong? How can I achieve this?

jscs
  • 63,694
  • 13
  • 151
  • 195
Charlie Pico
  • 2,036
  • 2
  • 12
  • 18

1 Answers1

8

You have to use es_MX locale first to convert the string to NSDate:

let string = "Jueves 10 Junio 1990"
let formatter = NSDateFormatter()
formatter.dateFormat = "EEEE dd MMMM yyyy"
formatter.locale = NSLocale(localeIdentifier: "es_MX")
if let date = formatter.dateFromString(string) {
    formatter.locale = NSLocale(localeIdentifier: "en_US")
    let dateString = formatter.stringFromDate(date)
}
riadhluke
  • 880
  • 6
  • 18