0

DateFormatter is acting funny to me:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM'/'DD'/'YYYY"
var dateString = "09/28/1989"
var date = dateFormatter.dateFromString(dateString)
println(date)

Returns:

"Optional(1988-12-24 13:00:00 +0000)"

Any kind of help will be appreciated.

Ivan Wang
  • 8,306
  • 14
  • 44
  • 56

1 Answers1

2

It looks like your date formatter is a bit off. "MM'/'DD'/'YYYY" reads "Padded month / day of the year (not month) / year of the current week (so Jan 1-6 could overlap and add side-effects)"

I'm guessing that what you're aiming for is "MM'/'dd'/'yyyy", which reads "Padded month / padded day of month / year"

For reference, here's the currently-used Unicode Technical Standard that Apple documents as their standard for iOS 7+ and OSX 10.9+ here

John
  • 2,675
  • 2
  • 18
  • 19
  • FYI - the use of `'` around the `/` are not needed. You only need to quote letters that you want to be treated as literals instead of format specifiers. – rmaddy Oct 31 '19 at 01:59