senty senty - 1 year ago 91
Swift Question

Converting String to NSDate Giving Wrong Date

I have a String that I converted using

and now I'm trying to convert it back, however when the UIPicker starts, it's giving me the wrong day

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM YYYY"

print(birthday) // logs 15 Jan 1992

let date = dateFormatter.dateFromString(birthday)
self.datePicker.setDate(date!, animated: true)

I tried hardcoding
"15 Feb 1992"
but still the same result. The date on UIDatePicker shows 22 Dec 1991 on Start.

If I use hardcore
10 Jan 1980
, it starts from 23 December 1979.

(I don't know if that's the case but I have
in UIPickerView whereas it's
for the strings.. I don't think though because while saving, it saves the right value)..

Answer Source

You are using the wrong format. You need dd MMM yyyy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download