18

I really don't get what is happening with this code, I'm trying to convert a string to date. What I don't understand is that the conversion works for most of the dates, but doesn't work specially for only 2 dates.

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "es")
dateFormatter.dateFormat = "dd 'de' MMMM"

dateFormatter.dateFromString("1 de octubre")
dateFormatter.dateFromString("2 de octubre")
dateFormatter.dateFromString("3 de octubre")
dateFormatter.dateFromString("4 de octubre")
dateFormatter.dateFromString("5 de octubre")
dateFormatter.dateFromString("6 de octubre")
dateFormatter.dateFromString("7 de octubre")
dateFormatter.dateFromString("8 de octubre")
dateFormatter.dateFromString("9 de octubre")
dateFormatter.dateFromString("10 de octubre")
dateFormatter.dateFromString("11 de octubre")
dateFormatter.dateFromString("12 de octubre")
dateFormatter.dateFromString("13 de octubre")
dateFormatter.dateFromString("14 de octubre")
dateFormatter.dateFromString("15 de octubre")
dateFormatter.dateFromString("16 de octubre")
dateFormatter.dateFromString("17 de octubre")
dateFormatter.dateFromString("18 de octubre")
dateFormatter.dateFromString("19 de octubre")
dateFormatter.dateFromString("20 de octubre")
dateFormatter.dateFromString("21 de octubre")
dateFormatter.dateFromString("22 de octubre")
dateFormatter.dateFromString("23 de octubre")
dateFormatter.dateFromString("24 de octubre")
dateFormatter.dateFromString("25 de octubre")
dateFormatter.dateFromString("26 de octubre")
dateFormatter.dateFromString("27 de octubre")
dateFormatter.dateFromString("28 de octubre")
dateFormatter.dateFromString("29 de octubre")
dateFormatter.dateFromString("30 de octubre")

I made this code to try out in Playground, but it reproduces some dates I'm getting in my production code. Take a look in the output that this code produces:

Playground output

As you can see in the image, it doesn't work for October 8 and 15. Works for all the others. I also tried August and September, and in those months all days work. I also tried to put the dates in Portuguese in pt-br locale, and also doesn't work for October 8 and 15. Is it explainable?


Update: @zneak code produces the following output for me:

output

In other words, stopped running October 8.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Glauco Neves
  • 3,483
  • 1
  • 24
  • 36

1 Answers1

8

You just need to set your date formatter calendar property:

Xcode 8.2.1 • Swift 3.0.2

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "es")
dateFormatter.dateFormat = "dd 'de' MMMM"
dateFormatter.date(from: "8 de octubre")   // "Oct 8, 2000, 1:00 AM"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • For me, it worked without setting a calendar. Can you reproduce the problem without setting it? – zneak Sep 05 '15 at 02:36
  • Yes without setting a calendar it returns nil. It depends on your location – Leo Dabus Sep 05 '15 at 02:36
  • I tried [setting a date formatter to every calendar out there](http://pastebin.com/yWs6nCWA) and the only ones with which the formatter fails to parse dates are Chinese, Hebrew, Islamic, Islamic Civil, Persian and Indian; they fail across the board, not just for two dates per month. What is the identifier of your initial calendar? – zneak Sep 05 '15 at 02:46
  • Sorry sneak I don't understand your question but I use ISO8601 calendar. The problem is when you don't assign anyone. Gregorian calendar also works for me here – Leo Dabus Sep 05 '15 at 02:48
  • My `NSDateFormatter`s are initialized to the Gregorian calendar. That's why I'm asking what yours is initialized to: what's `dateFormatter.calendar.calendarIdentifier` for you? – zneak Sep 05 '15 at 02:50
  • @zneak dateFormatter.calendar.calendarIdentifier prints "gregorian" too. – Glauco Neves Sep 05 '15 at 02:51
  • 3
    And setting it to Gregorian again fixes the problem? That doesn't really look right. Consider filing a bug report if this really works. – zneak Sep 05 '15 at 02:54
  • @zneak Doing this makes the code work: dateFormatter.calendar = NSCalendar(calendarIdentifier: dateFormatter.calendar.calendarIdentifier) – Glauco Neves Sep 05 '15 at 02:58