1

I tried to use NSDateFormatter like following.
On simulator, it works fine. -> 2015-06-01
But, on actual equipment, it does't work. it'll be nil.

let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
            formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
if let date = formatter.dateFromString(d) {
    formatter.dateFormat = "yyyy-MM-dd"
    self.dateLabel?.text = formatter.stringFromDate(date)
}

Is this iPhone's problem?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
naohide_a
  • 1,116
  • 2
  • 13
  • 30

1 Answers1

0

Could be an issue with locale and 12/24-hour time handling. Try forcing formatter's locale to be consistent for the date you are receiving.

let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
if let date = formatter.dateFromString(d) {
    formatter.locale = NSLocale.currentLocale()
    formatter.dateFormat = "yyyy-MM-dd"
    self.dateLabel?.text = formatter.stringFromDate(date)
}

Check these question/answers for more info.

Community
  • 1
  • 1
Nikita Kukushkin
  • 14,648
  • 4
  • 37
  • 45