2
votes

I am trying to convert a date from a String to NSDate with the following code:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E, dd MMM yyyy, KK:mma"
date = dateFormatter.dateFromString(strDate)

I have also tried the following formats:

  1. EEE, dd MMM yyyy, hh:mma
  2. EEE, dd MMM yyyy, KK:mm

but the out put is always nil

Converted Tue, 12 Jan 2016, 7:30am to nil with the formatter E, dd MMM yyyy, KK:mma

I have also considered the follow StackOverflow posts:

  1. String to NSDate in Swift
  2. How can I convert string date to NSDate?

and I have been using this link as a reference for the date formats

1
Cannot reproduce, works on my machine.luk2302
What locale is set on your device? Are you aware that the code relies on the fact that the current locale language is set to English?Sulthan

1 Answers

5
votes

The typical reason is that your device is not set to English language. Note that parsing strings like Tue or Jan is language specific. That means you have to make sure the formatter has a correct language set, e.g.:

dateFormatter.locale = NSLocale(localeIdentifier: "en")